簡體   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