藉助case,實現更豐富的分組查詢統計

根據fileD6的前4位分組    分別統計該組  5種企業類型fileD31的數量

create or replace view jyjc_bycity as
select
 substr(fileD6,1,4)  site,
count( case when fileD31 like '%國有%' then 1 end) numg,
count( case when fileD31 like '%集體%' then 1 end) numj,
count( case when fileD31 like '%股份%' then 1 end) numm,
count( case when fileD31 like '%海外%' then 1 end) numw,
count( case when fileD31 like '%其他%' then 1 end) numq
from tab_jyjc_data t  group by     substr(fileD6,1,4) order by substr(fileD6,1,4)

case語句對企業類型進行篩選 

結果:



類似問題 還有  根據班級分組分別統計班級內男和女的數量 

 select class ,  count(case when Sex='男' then 1 end) as 男,
                 count(case when Sex='女' then 1 end) as 女 
 from Tab group by class



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章