我有一个类似这样的查询,它返回每个城市案例的行数。
select
case edition_id
when 6 then 'DELHI'
when 50 then 'AHMEDABAD'
when 4 then 'HYDERABAD'
when 25 then 'KOLKATA'
when 51 then 'BANGALORE'
when 5 then 'MUMBAI'
when 24 then 'CHENNAI'
end as CITY,
count(*) as Total
from #tmptab1
group by edition_id
drop table #tmptab1
结果如下所示:
CITY Total
MUMBAI 1
DELHI 28
CHENNAI 1
KOLKATA 35
AHMEDABAD 3
如果从一个城市没有返回任何行,那么该城市在最终结果中将被省略。
我希望得到的结果是:
CITY Total
MUMBAI 1
DELHI 28
CHENNAI 1
KOLKATA 35
AHMEDABAD 3
BANGALORE 0 -- if no result from bangalore display zero.
如何做到这一点?
我尝试过。
case count(*)>0 then count(*) else 0 end as Total
但它不起作用。