簡體   English   中英

SQL(DB2)-如果找不到記錄,則返回零

[英]SQL (DB2) - Return zero if no records found

如何更改此查詢以在沒有找到行的每個區域中顯示零?

    proc sql;
create table test as
select count(acct_num) as total_count,
       region
    from
        table1
    group by region
    ;
run;

理想情況下,我希望看到以下內容:

Region1   500
Region2   0
Region3   20
Region4   0

假設您有想要的所有區域的表,則可以使用left join

select r.region, count(t.acct_num) as total_count
from regions r left join
     table1 t
     on r.region = t.region
group by r.region;

如果沒有這樣的表,則可以通過以下操作動態創建一個表:

select r.region, count(t.acct_num) as total_count
from (select 'region1' as region from sysibm.sysdummy1 union all
      select 'region2' from sysibm.sysdummy1 union all
      . . .
     ) r left join
     table1 t
     on r.region = t.region
group by r.region;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM