Sql server 如何在sql中添加多个sum语句如果值为零,则它将总金额返回为零如何添加多个sum语句

Sql server 如何在sql中添加多个sum语句如果值为零,则它将总金额返回为零如何添加多个sum语句,sql-server,Sql Server,(金额(当国籍为“沙特”和“达曼中央”且状态为空时的情况)(当前工资)结束)(金额(当国籍为“沙特”和“达曼中央”时的情况,以及从in(“达曼中央”、“达曼东”、“吉达SMPT”、“朱拜勒”、“哈迪德MTO”、“哈迪德FPH”、“伊本拉什德”)进行的内部转账(当前工资)结束)+((金额(当国籍为“沙特”时,从“Dammam Central”进行内部转账,然后(当前工资)结束))作为Dammam Central,这是您要找的吗 isnull((sum (case when NATIONALITY

(金额(当国籍为“沙特”和“达曼中央”且状态为空时的情况)(当前工资)结束)(金额(当国籍为“沙特”和“达曼中央”时的情况,以及从in(“达曼中央”、“达曼东”、“吉达SMPT”、“朱拜勒”、“哈迪德MTO”、“哈迪德FPH”、“伊本拉什德”)进行的内部转账(当前工资)结束)+((金额(当国籍为“沙特”时,从“Dammam Central”进行内部转账,然后(当前工资)结束))作为Dammam Central,

这是您要找的吗

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,

注意:供参考

使用合并(字段名,0)=casesum(字段名)=0,然后sum()否则sum()结束

上面的样本

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

这意味着如何使用上面的一个