简体   繁体   English

在 SQL 服务器中使用 SUM Function 时如何显示 Null 值

[英]How to show Null Value when using SUM Function in SQL Server

I have issue with my script in SQL Server, i need to show Total of 3 Columns with NULL Values我在 SQL 服务器中的脚本有问题,我需要显示 NULL 值的总共 3 列我的查询

here is my query:这是我的查询:

SELECT 
    Bucket,
        (SELECT SUM (OutstandingPrincipal)  FROM example
        WHERE [status] = 'Covid')
    AS Covid,
        (SELECT SUM (OutstandingPrincipal) FROM example
        WHERE [status] LIKE '%Disbu%')
    AS Disburs_After,
        (SELECT SUM (OutstandingPrincipal) FROM example
        WHERE [status] = 'Non Covid')
    AS Non_Covid
FROM example
WHERE Bucket IS NOT NULL
GROUP BY 
    Bucket

how is it possible?这怎么可能? thanks in advance提前致谢

If I'm understanding your data correctly, no need for all those subqueries, you can just use conditional aggregation :如果我正确理解您的数据,则不需要所有这些子查询,您可以使用conditional aggregation

select bucket,
       sum(case when status = 'Covid' then OutstandingPrincipal end) as covid,
       sum(case when status like '%Disbu%' then OutstandingPrincipal end) as Disburs_After,
       sum(case when status = 'Non Covid' then OutstandingPrincipal end) as Non_Covid
from example
where bucket is not null
group by bucket

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM