Thursday, February 27, 2014

Strange problem with case statement

Try the Below one It working as expected



Declare @T as Table
(
Months int,
Cash Float
)
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 'Total' as Months
,Cast(max(case when Cash = '4.13' then Cash end) as varchar(20)) [1]
,Cast(max(case when Cash = '46.02' then Cash end) as varchar(20)) [2]
,Cast(max(case when Cash = '46.02' then Cash end) as varchar(20)) [3]
,Cast(max(case when Cash = '5.31' then Cash end) as varchar(20)) [4]
,Cast(max(case when Cash = '5.31' then Cash end) as varchar(20)) [5]
,Cast(max(case when Cash = '51.33' then Cash end) as varchar(20)) [6]
,Cast(max(case when Cash = '393.53' then Cash end) as varchar(20)) [7]
,Cast(max(case when Cash = '393.53' then Cash end) as varchar(20)) [8]
,Cast(max(case when Cash = '46.02' then Cash end) as varchar(20)) [9]
,Cast(max(case when Cash = '51.33' then Cash end) as varchar(20)) [10]
,Cast(max(case when Cash = '57.82' then Cash end) as varchar(20)) [11]
,Cast(max(case when Cash = '32.45' then Cash end) as varchar(20)) [12]
,Cast(max(case when Cash = '0' then Cash end) as varchar(20)) [13]
,Cast(max(case when Cash = '0' then Cash end) as varchar(20)) [14]
,Cast(max(case when Cash = '0' then Cash end) as varchar(20)) [15]
from @T




Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.


No comments:

Post a Comment