如果值为零,如何在sql中添加多个sum语句,它将总数量返回到零,如何在多个sum语句中添加

时间:2018-02-28 08:08:37

标签: sql-server

(总和(国民队的情况!='沙特'和DIVISION ='达曼中央'和STATUS为空然后(current_salary)结束)) - (总和(民族时的情况!='沙特'和DIVISION ='达曼中心'和'internal_transfer_from in('Dammam Central','Dammam East','Jeddah SMPT','Jubail','hadeed MTO','Hadeed FPH','IBN Rushd')然后(current_salary)结束))+((总和) (当NATIONALITY!='Saudi'和internal_transfer_from ='Dammam Central'然后(current_salary)结束时)))作为Dammam_Central,

2 个答案:

答案 0 :(得分:1)

这就是你要找的东西

isnull((sum (case when NATIONALITY != 'Saudi' and DIVISION='Dammam Central' and STATUS is null then (current_salary) end)),0)-
isnull((sum (case when NATIONALITY != 'Saudi' and DIVISION='Dammam Central' and internal_transfer_from in ('Dammam Central','Dammam East','Jeddah SMPT','Jubail','hadeed MTO','Hadeed FPH','IBN Rushd') then (current_salary) end)),0)+
isnull(((sum (case when NATIONALITY != 'Saudi' and internal_transfer_from='Dammam Central' then (current_salary) end))),0) as Dammam_Central,

注意:仅供参考Handling Nulls

答案 1 :(得分:0)

使用合并(fieldname,0)= casesum(fieldnames)= 0然后sum()else sum()end

上面的示例

COALESCE(sum (case when NATIONALITY != 'Saudi' and DIVISION='Dammam Central' and STATUS is null then (current_salary) end)),0)- COALESCE(sum (case when NATIONALITY != 'Saudi' and DIVISION='Dammam Central' and internal_transfer_from in ('Dammam Central','Dammam East','Jeddah SMPT','Jubail','hadeed MTO','Hadeed FPH','IBN Rushd') then (current_salary) end)),0)+(COALESCE(sum (case when NATIONALITY != 'Saudi' and internal_transfer_from='Dammam Central' then (current_salary) end)),0) as Dammam_Central