Thursday, February 27, 2014

Strange problem with case statement

See full illustration here



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


declare @monthList varchar(max),@sql varchar(max)
set @monthList= STUFF((select ',[' + CAST(Months as varchar(2)) + ']' FROM #t for xml path('')),1,1,'')

set @sql = 'SELECT ' + @MonthList + 'FROM #T PIVOT(MAX(Cash) FOR Months IN (' + @monthList + '))p'

EXEC (@sql)
DROP TABLE #T







Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://ift.tt/19nLNVq http://ift.tt/1iEAj0c


No comments:

Post a Comment