Skip to content
Advertisement

Query to “PIVOT” the results of SalesAmount & CumulativeMonthly for specific YEARS

I have a database table that captures every Sales Transaction:

I need to run a T-SQL query to produce a Sales Report, that shows :

(1) Sales (monthly) (2) Cumulative Monthly Sales

These will be two columns per year; PIVOT the results for next year on next two neighbor columns (structure as ExcelSheet template below). The results will extend horizontally for all Years in the transaction data.

enter image description here

Advertisement

Answer

You could use conditional aggegation and window functions, as follows:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement