I have this query and would like to pivot the results in such a way that I get the AirlineCode
in y-axis
and the Month
in the x-axis
for a certain year. The query is doing just the right thing for me now but it is kind of exhausting to copy everything to Excel and sort it by myself.
;WITH MonthsStartDates AS
(
SELECT
MonthStartDate = CONVERT(DATE, '2018-01-01')
UNION ALL
SELECT
MonthStartDate = DATEADD(MONTH, 1, M.MonthStartDate)
FROM
MonthsStartDates AS M
WHERE
M.MonthStartDate <= '2020-01-01'
)
SELECT
AirlineCode,
Year = DATEPART(YEAR, M.MonthStartDate),
Month = DATEPART(MONTH, M.MonthStartDate),
AppUser = COUNT(DISTINCT AppUser.Id)
FROM
[followme.aero.live].[dbo].[AppUser]
INNER JOIN
[followme.aero.live].[dbo].[UserInAppPurchase] ON AppUser.Id = UserInAppPurchase.UserId
INNER JOIN
MonthsStartDates AS M ON PurchaseDate < M.MonthStartDate
AND ValidTo > M.MonthStartDate
GROUP BY
AirlineCode,
DATEPART(YEAR, M.MonthStartDate),
DATEPART(MONTH, M.MonthStartDate)