私はクエリ(MS SQL Server)に問題があり、以下のようなことができるかどうか、またそれを正しく行う方法があるかどうかを知りたい。結果のない行を表示する(groupby - count)
これはクエリです:
select numTenants, count(codSite) numSites
from (select case count(st1.name) when 0 then '0T'
when 1 then '1T'
when 2 then '2T'
when 3 then '3T'
when 4 then '4T'
else 'More than 4T' end numTenants, os1.siteCode as codSite
from fl_OperativeSite os1 left join fl_SiteTenant st1
on st1.fkOperativeSite=os1.pkOperativeSite
where os1.siteType='A' and os1.externalInfrastructure=2
group by os1.siteCode) groups
group by numTenants
order by numTenants
、これは結果である:明らかにnumTenantsとして0と "サイト" がありませんので
numTenants numSites
1T 2957
2T 553
3T 1503
4T 423
More than 4T 131
。
質問したいのは、次のように結果を表示する方法はありますか?
numTenants numSites
0T 0
1T 2957
2T 553
3T 1503
4T 423
More than 4T 131
ありがとうございました!
あなたが「0」で外部結合と置き換えるNULL結果を使用することができます。 – ali
これらの番号範囲は頻繁に使用しますか?次に、テキスト(「4T以上」など)、範囲(例:from = 5、to = null)とソートキー(例:5)を含むテーブルを用意します。これにより、そのようなクエリが簡素化されます。 –