Complex Logic- Finding Renewals on Licenses - sql

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.

Related

From one record to more records that represent mm/yyyy

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

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"

How to use SAS/SQL to create a table with certain conditions from a dataset

I have a dataset with ID and event_year (event meaning something happened that year, a person has more than one record in this table with more than one event year eg. ID 1 can have three entries with event_year 2017, 2018, 2019 ). Example dataset like:
ID event_year
1 2017
1 2018
1 2019
2 2018
2 2017
ID
event_year
1
2017
1
2018
1
2019
2
2018
2
2017
I need to get a table from this of all ID where the event_year is between 2017 and 2021 to make a frequency table counting people with event_year at set years 2017, 2018, 2019, 2020, 2021 (these are the columns refer to as study year x).
Year frequency
2017 2
2018 2
2019 1
2020 1
2021 0
Year
frequency
2017
2
2018
2
2019
1
2020
1
2021
0
Another condition is for the study year x if a person didnt have an event_year in x but had event_year x-1 they will be included in the frequency of year x, for example the ID 1 above should be included in frequency of once in each 2017, 2018, 2019 and 2020- because following the condition above for year 2020 they didnt have event_year in 2020 but did in 2019 so will be included in 2020. I apologise if this is confusing and would be happy to clarify
If I understood your question, this should work:
data have;
input ID event_year;
datalines;
1 2017
1 2018
1 2019
2 2018
2 2017
3 2017
3 2020
;
run;
For the next step (your additional requirement of being included a year after last event) we need data grouped by ID.
proc sort data=have;
by ID;
run;
We just add extra rows to a table, where a year is last year + 1.
data have;
set have;
output;
by ID;
if last.ID then do;
ID=ID;
event_year=event_year+1;
output;
end;
run;
Now we just check how many different IDs every year had. If you want to check only for certain years, just add a where clause (for example, where event_year in (2017, 2018, 2019, 2020, 2021) ).
proc sql;
create table want as
select distinct event_year, count(distinct ID) as frequency
from have
group by event_year
;
run;

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

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.