I can’t seem to get my head around this problem where I think I need a combination of pivot and unpivot in SQL Server 2008:
I have a table as follows:
Sale | Month | Count | Budgeted | Actual
------------------------------------------------
NewSale | 1 | 120 | 45.23 | 50.10
NewSale | 2 | 30 | 3.10 | 1.2
NewSale | 3 | 70 | 45.00 | 100.32
I need to pivot so that the months are as columns, but unpivot so I get the Count, Budgeted, Actual as rows, so it is like so…
Type | 1 | 2 | 3
-----------------------------------
Count | 120 | 30 | 70
Budgeted | 45.23 | 3.10 | 45.00
Actual | 50.10 | 1.2 | 100.32
I’ve tried this so far, but I can’t work out how to put the pivot in there:
select
*
from YTD
pivot
(
sum([Count]), sum([Budgeted]), sum([Actual])
for [Month] in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
)
as figures
This gives me a syntax error as you can’t have more than one calculation in the pivot (as far as what I understood from the error.
Help!!!
Try on SE-Data.