Tuesday, March 3, 2015

Setting a calculated field to 1

Hi, Please take a look and tweak accordingly.



Create table #test (Acode char(1), V1 int, V2 int, V3 int)
Insert into #test values('A',3,4,6),('B',0,null,0),('C',2,3,3),
('D',NULL,NULL,NULL), ('E',5,NULL,NULL)

Select Acode,
Case
When sum(ISNULL(v1, 0)+ISNULL(v2, 0)+ ISNULL(v3,0) ) > 0 Then sum( ISNULL(v1, 0)+ISNULL(v2, 0)+ ISNULL(v3,0) )
Else 1
End as TargetSum ,
case
When sum(coalesce(v1, 0)+coalesce(v2, 0)+ coalesce(v3,0) ) > 0 Then sum( coalesce(v1, 0)+coalesce(v2, 0)+ coalesce(v3,0) )
--sum( ISNULL(v1, 0)+ISNULL(v2, 0)+ ISNULL(v3,0) ) > 0 Then sum( ISNULL(v1, 0)+ISNULL(v2, 0)+ ISNULL(v3,0) )
Else 1
End as TargetMax
from #test
Group by Acode

drop table #test


No comments:

Post a Comment