From one record to more records that represent mm/yyyy - sql

Let's say that we have this table
Employee
EmploymentStarted
EmploymentEnded
Sara
20210115
20210715
Lora
20210215
20210815
Viki
20210515
20210615
Now what I need is a table that we can see all the employees that we had each month. For example, Sara started on January 15th 2021 and she left the company on July 15th 2021. This means that she has been with us during January, February, March, April, May, June and July.
The result table should look like this:
Month
Year
Employee
January
2021
Sara
February
2021
Sara
February
2021
Lora
March
2021
Sara
March
2021
Lora
April
2021
Sara
April
2021
Lora
May
2021
Sara
May
2021
Lora
May
2021
Viki
June
2021
Sara
June
2021
Lora
June
2021
Viki
July
2021
Sara
July
2021
Lora
August
2021
Lora
How can I get a table like this in SQL?
I tried a group by, but it does not seem to be the right way to do it

It would be interesting to find out in practice how much performance decreases when using recursion. In this case calendarTable contain less about 12 records per year. Most part of query cost is JOIN to Employee (staff) table.
with FromTo as (
select min(EmploymentStarted) fromDt, eomonth(max(EmploymentEnded)) toDt
from staff
)
--,FromTo as(select fromDt=#fromDt,toDt=#toDt)
,rdT as (
select 1 n,fromDt bM,eomonth(fromDt) eM
,fromDt, toDt
from FromTo
union all
select n+1 n,dateadd(day,1,eM) bM,eomonth(dateadd(month,1,bM)) eM
,fromDt,toDt
from rdT where dateadd(month,1,bM)<toDt
)
select month(bM) as 'Month',year(bM) as 'Year',Employee --*
from rdT left join staff s on s.EmploymentEnded>=bM and s.EmploymentStarted<=eM
Fiddle

Related

Sort SQL by value

I have data like this:
Customer ID
Name
Type
Last Submit
1
Patricio
C
January 2022
2
Dale
A
June 2022
3
Yvonne
C
July 2022
4
Pawe
C
JUne 2022
5
Sergio
B
August 2022
6
Roland
C
August 2022
7
Georg
D
November 2022
8
Catherine
D
October 2022
9
Pascale
E
October 2022
10
Irene
A
November 2022
How to sort type A out of the queue first like A,B,C,D,E,F, then the last submit is at the top.
The example output:
Customer ID
Name
Type
Last Submit
10
Irene
A
November 202[![enter image description here][1]][1]2
1
Dale
A
June 2022
5
Sergio
B
August 2022
6
Roland
C
August 2022
3
Yvonne
C
July 2022
4
Pawe
C
June 2022
1
Patricio
C
January 2022
7
Georg
D
November 2022
8
Catherine
D
October 2022
9
Pascale
E
October 2022
So basically you want to sort by 2 different columns, this is detailed in this other answer: SQL multiple column ordering
In your example you would do
ORDER BY type, last_submit
Hi you can use simple order by in postgresql
like this
SELECT
*
FROM
table (your table name)
ORDER BY
type ASC, last_submit DESC;
In this case, you need to sort your query using the two columns in order.
Add this part to the end of your query.
ORDER BY type, last_submit DESC;
Check out this question "SQL multiple column ordering"

Identify overlap percent of IDs between 2 dates in same table

I have a table of names with two different dates. I want to know the count of names that are occurring between the two dates and the overlap percentage.
This is the output format that is desired. I am not looking for dates in between. I am looking for records that are in July 05 and also in August 10. Overlap percentage for each id would be - count of records in July 5 and also August 10/count of records on July 5.(Actual table has dates in date datatype).
Overlap % will always be less than or equal to 100 since count of records existing on July 5 as well as August 10 will always be <=count of records on July 5.
id
Count on July 05
Count of IDs from July 05 included in August 10
% overlap
ABC
BCD
CDE
DEF
EFG
Rough version of the input table
id
type
Group
date
ABC
Mobile
1
July 5
BCD
Mobile
1
July 5
ABC
Desktop
1
August 10
CDE
Mobile
2
July 5
BCD
Mobile
2
August 10
As I understood from your comments, the overlap will be the minimum count value of the two dates, i.e. for ABC if we have 6 in July and 2 in August the overlap will be 2, and if we have 3 in July and 5 in August the overlap will be 3.
If that is the case then you may use the following query tested on MS SQL Server 2019:
SELECT t.id, t.[Count on July 05],
CASE
WHEN t.[Count on July 05]<= t.[Count of August 10] THEN t.[Count on July 05]
WHEN t.[Count on July 05]> t.[Count of August 10] THEN t.[Count of August 10]
END AS [Count of IDs from July 05 included in August 10],
CASE
WHEN t.[Count on July 05]<= t.[Count of August 10] THEN CAST(t.[Count on July 05]*1.00/t.[Count on July 05] * 100 AS DECIMAL(18, 2))
WHEN t.[Count on July 05]> t.[Count of August 10] THEN CAST(t.[Count of August 10]*1.00/t.[Count on July 05] * 100 AS DECIMAL(18, 2))
END AS [% overlap]
FROM(
SELECT id,
COUNT(CASE WHEN [tdate] IN ('July 5') THEN 1 END) as [Count on July 05],
COUNT(CASE WHEN [tdate] IN ('August 10') THEN 1 END) as [Count of August 10]
FROM [Tbl]
GROUP BY id) t
I hope that is what you are looking for.

Count the number of records for each 1st of the month in SQL

I have a dataset where I would like to query and obtain output of a count of records for the first of every month.
Data
name date1
hello july 1 2018
hello july 1 2018
hello july 10 2018
sure august 1 2019
sure august 1 2019
why august 20 2019
ok september 1 2019
ok september 1 2019
ok september 1 2019
sure september 5 2019
Desired
ID MONTH Day YEAR
2 July 1 2018
2 August 1 2019
3 September 1 2019
We are only counting the records from the 1st of each month
Doing
USE [Data]
SELECT COUNT(*) AS ID , MONTH(date1) AS MONTH, YEAR(date1) AS YEAR
FROM dbo.data1
GROUP BY MONTH(date1), YEAR(date1)
ORDER BY YEAR ASC
This only outputs the year and month
Any suggestion is appreciated
Assuming you are using the implicit conversion for date
Example
SELECT COUNT(*) AS ID,
DATENAME(MONTH,date1) AS MONTH,
DATEPART(DAY,date1) as DAY,
YEAR(date1) AS YEAR
FROM dbo.data1
WHERE DAY(date1)=1
GROUP BY YEAR(date1),DATENAME(MONTH,date1),DATEPART(DAY,date1)
ORDER BY YEAR ASC
Results
ID MONTH DAY YEAR
2 July 1 2018
2 August 1 2019
3 September 1 2019

Complex Logic- Finding Renewals on Licenses

Need help in querying the below logic:
I have to calculate renewals. An org can order up to 5 licenses in total from us
License Table 2018
Year
Licenses
2018
A
2018
A
2018
B
2018
B
2018
C
2019
A
2019
A
2019
B
License Table 2019:
Year
Licenses
2019
A
2019
A
2019
B
Result:
License
Renewal Percentage
A
100%
B
50%
C
0%
More conditions:
Suppose the 2 order dates are 6/30/2019 and 7/1/2020, which are in FY19 and FY21 respectively. Even though the 2nd order is barely more than 1 year later, it is not treated as a renewal.

Predict future Data trends with current Data using sql script and them plot them over SSRS

I have been asked to create a trendline in SSRS, this trendline will the predicted future value based on current year data.
Here I have data of year 2018 and I needed to predict the trends of ClaimVolume for year 2019.
Please find the data
Month Month Name Year ClaimVolume
1 January 2018 13746
2 February 2018 13412
3 March 2018 15143
4 April 2018 15655
5 May 2018 15190
6 June 2018 15365
7 July 2018 18943
8 August 2018 24305
9 September 2018 18893
10 October 2018 26659
11 November 2018 18696
12 December 2018 22367
Please help me in providing SQL query for the above task.