I hope my description is clear. I have a single view that contains entries based on Level and Status. I wrote a query that counts each Level by its type and Status and then calculates the percent of the total of each Level within a given year using multiple Select statements in a single query. I based my query on this logic: Multiple select statements in a Single query.
My logic:
SELECT Table1.[Level Description]
, Table1.[Level Status]
, Table1.[Level Count]
, Table2.[Level Total]
, CAST(Table1.[Level Count] / Table2.[Level Total] AS DECIMAL(3, 2)) * 100 AS 'LVL %'
FROM
(
SELECT LEVEL_DESC AS 'Level Description'
, LEVEL_STATUS 'Level Status'
, (CASE
WHEN LEVEL_DESC IN ('LEVEL 1', 'LEVEL 2', 'LEVEL 3', 'LEVEL 4')
AND LEVEL_STATUS IN ('A', 'I', 'R') THEN
COUNT(LEVEL_STATUS) * 1.00
ELSE
0
END
) AS 'Level Count'
FROM dbo.My_Level_View
WHERE (DUE_DATE BETWEEN '2019-01-01' AND '2019-12-31')
AND LEVEL_SUB = 'I'
GROUP BY LEVEL_DESC
, LEVEL_STATUS
) AS [Table1]
, (
SELECT LEVEL_DESC AS 'Total Description'
, (CASE
WHEN LEVEL_DESC IN ('LEVEL 1', 'LEVEL 2', 'LEVEL 3', 'LEVEL 4') THEN
COUNT(LEVEL_STATUS) * 1.00
ELSE
0
END
) AS 'Level Total'
FROM dbo.My_Level_View
WHERE (DUE_DATE BETWEEN '2019-01-01' AND '2019-12-31')
AND LEVEL_SUB = 'I'
GROUP BY LEVEL_DESC
) AS [Table2]
WHERE Table1.[Level Description] = Table2.[Total Description];
The query works well and gives me the result I need:
However, I would like to extend it to work with any record within a two year window from today's date and group the results by year.
I have tried using
WHERE DATEDIFF(YEAR, DUE_DATE, GETDATE()) < 2
in place of the hard coded date range
WHERE (DUE_DATE BETWEEN '2019-01-01' AND '2019-12-31')
When I do this it counts each Level by it's Type within the the last two years and then performs the calculations. I would like to separate the results by year, e.g., 2019 and 2020 as shown in the image.
What I have scripted may not be the best solution. How can I make my query separate the results by year?