【问题标题】:How to add the multiple sum statement in sql if value has zero it returns total amount to zero how to add in multiple sum statement如果值为零,如何在sql中添加多个sum语句它返回总额为零如何添加多个sum语句
【发布时间】:2018-02-28 08:08:37
【问题描述】:

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

【问题讨论】:

    标签: sql-server


    【解决方案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

    【讨论】:

      【解决方案2】:

      使用 coalesce(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
      

      【讨论】:

      • 上面那个是什么意思
      猜你喜欢
      • 1970-01-01
      • 2019-09-07
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 2021-12-29
      相关资源
      最近更新 更多