SQL sum with condition

前端 未结 3 1451
情话喂你
情话喂你 2020-11-30 23:46

I currently have a large SQL statement which i add the following line to in order to get the total cash for each transaction ID (which are unique):

select su         


        
3条回答
  •  温柔的废话
    2020-11-30 23:55

    Try moving ValueDate:

    select sum(CASE  
                 WHEN ValueDate > @startMonthDate THEN cash 
                  ELSE 0 
               END) 
     from Table a
    where a.branch = p.branch 
      and a.transID = p.transID
    

    (reformatted for clarity)

    You might also consider using '0' instead of NULL, as you are doing a sum. It works correctly both ways, but is maybe more indicitive of what your intentions are.

提交回复
热议问题