繁体   English   中英

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

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

我在 SQL 服务器中的脚本有问题,我需要显示 NULL 值的总共 3 列我的查询

这是我的查询:

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

这怎么可能? 提前致谢

如果我正确理解您的数据,则不需要所有这些子查询,您可以使用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