[英]NULL value returned when using SUM function on two columns while populating table in SQL Server 2008
[英]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.