Thursday, February 27, 2014

Strange problem with case statement

You may try with PIVOT:



CREATE TABLE #T
(
Months int,
Cash decimal(10,2)
)
insert #T
select 1, 4.13 union all
select 2 ,46.02 union all
select 3 ,46.02 union all
select 4 ,5.31 union all
select 5 ,5.31 union all
select 6 ,51.33 union all
select 7 ,393.53 union all
select 8 ,393.53 union all
select 9 ,46.02 union all
select 10, 51.33 union all
select 11 ,57.82 union all
select 12 ,32.45 union all
select 13 ,0 union all
select 14 ,0 union all
select 15 ,0

SELECT 'Cash' AS Cash,
[1], [2], [3], [4],[5],[6],[7],[8],[9],[10],[11],[12]
FROM
(SELECT months,Cash
FROM #T) AS SourceTable
PIVOT
(
MAX(Cash)
FOR Months IN ([1], [2], [3], [4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS PivotTable;

Drop table #T


No comments:

Post a Comment