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

问题描述 投票:0回答:2

(总和(国民的情况!='沙特'和DIVISION ='达曼中心'和STATUS为空然后(current_salary)结束)) - (总和(国民的情况!='沙特'和DIVISION ='达曼中央'和internal_transfer_from在('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,

sql-server
2个回答
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


0
投票

使用coalesce(fieldname,0)= casesum(fieldnames)= 0然后sum()else 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
© www.soinside.com 2019 - 2024. All rights reserved.