Link to home
Start Free TrialLog in
Avatar of shpresa
shpresaFlag for United States of America

asked on

Sum Case in MS SQl

Ho can I make a sum case statement work

SUM(
            (case
                    when a.var1 = 9 then 0 else a.var1  +
                        when a.var2 = 9 then 0 else a.var2          )
              
      ) as sumbeg,
Avatar of Jason Yousef
Jason Yousef
Flag of United States of America image

I'm not sure what you're trying to do here !! the query doesn't to make sense

anyway test that, if it didn't 'work fr, please attache an example..
Select A + B from (

Select 'A' = case when a.var1 = 9 then 0 else a.var1 END , 
	'B' = Case when a.var2 = 9 then 0 else a.var2 END

from Table
)X

Open in new window

Avatar of shpresa

ASKER

I have a big  query in a store procedur. So I am taking only the part that I need to change.
This is how it was originally,
Select A, B, C, D,
      SUM((ISNULL(s.var1,0)) + (ISNULL(s.var2,0))  as sumbeg,
       
        SUM((ISNULL(a.var1,0)) + (ISNULL(a.var2,0))  as summid,


All I need to do is to exclude the value  s.var1 = 9 and make it 0 the same for var2



ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shpresa

ASKER

thanks a lot. U are awesome