Stuck on Unpivot / Pivot Combo SQL Server 2008 -
i can't seem head around problem think need combination of pivot , unpivot in sql server 2008:
i have table 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 pivot months columns, unpivot count, budgeted, actual rows, 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 far, can't work out how put pivot in there:
select * ytd pivot ( sum([count]), sum([budgeted]), sum([actual]) [month] in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) ) figures
this gives me syntax error can't have more 1 calculation in pivot (as far understood error.
help!!!
declare @t table ( sale varchar(10), [month] int, [count] int, budgeted money, actual money ) insert @t values ('newsale', 1, 120, 45.23, 50.10), ('newsale', 2, 30, 3.10, 1.2), ('newsale', 3, 70, 45.00, 100.32) select [type], [1], [2], [3] ( select [month], cast([count] money) [count], budgeted, actual @t ) t unpivot ( value [type] in ([count], budgeted, actual) ) u pivot ( sum(value) [month] in ([1], [2], [3]) ) p
try on se-data.
Comments
Post a Comment