場景介紹
有一張表 table
字段有KCDM HGBJ
其中 HGBJ 只可能有兩種值,要么是1 要么是2
現在要根據 KCDM 分組查詢統計出 HGBJ=1 有多少條, HGBJ=2有多少條
SQL如下:
select KCDM
,count(HGBJ) as num
,count(case when HGBJ='1' then XH else null end) as num1
,count(case when HGBJ='2' then XH else null end) as num2
from table
group by KCDM
