I have the following dataset which looks like this:
ID Date PT Type
1 1/01/2018 Excellent
2 1/11/2018 Good
3 2/1/2018 Excellent
4 4/18/2018 Fair
5 7/23/2018 Bad
6 1/12/2019 Excellent
6 3/18/2019 Excellent
6 8/2/2019 Good
I want to create a crosstab table that aggregates the data based on year and month which looks like this (or close to it)
Year Month Excellent Good Fair Bad
2018 January 2 5 4 3
2018 February 7 0 2 9
....
I used the following stackoverflow link to get the framework of the code I need but am running into issues because I need to create two alias variable based on the Date
variable.
The code I have now looks like this but I'm not sure how to complete it.
SELECT PT_TYPE ,
sum(case when PT_TYPE = 'Excellent' then 1 else 0 end) [Excellent],
sum(case when PT_TYPE = 'Good' then 1 else 0 end) [Good],
sum(case when PT_TYPE = 'Fair' then 1 else 0 end) [Fair]
from
(
select count(*) over(partition by Date)
from ED_TAT_MASTER
)