oracle 分類統計sql總結

一、根據年份統計12個月的彙總信息,沒有數據的月顯示0,重點是如何分別顯示12個月post

select train.*,terri.territoryname from  (
SELECT s.sponsor,s.regioncode, 
count(case when TO_CHAR(s.START_TIME, 'MM')='01' then 'm1' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='01' then SIGNUP_NUMBERS else '0' end) m1,
count(case when TO_CHAR(s.START_TIME, 'MM')='02' then 'm2' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='02' then SIGNUP_NUMBERS else '0' end) m2, 
count(case when TO_CHAR(s.START_TIME, 'MM')='03' then 'm3' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='03' then SIGNUP_NUMBERS else '0' end) m3,
count(case when TO_CHAR(s.START_TIME, 'MM')='04' then 'm4' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='04' then SIGNUP_NUMBERS else '0' end) m4,
count(case when TO_CHAR(s.START_TIME, 'MM')='05' then 'm5' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='05' then SIGNUP_NUMBERS else '0' end) m5, 
count(case when TO_CHAR(s.START_TIME, 'MM')='06' then 'm6' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='06' then SIGNUP_NUMBERS else '0' end) m6, 
count(case when TO_CHAR(s.START_TIME, 'MM')='07' then 'm7' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='07' then SIGNUP_NUMBERS else '0' end) m7, 
count(case when TO_CHAR(s.START_TIME, 'MM')='08' then 'm8' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='08' then SIGNUP_NUMBERS else '0' end) m8, 
count(case when TO_CHAR(s.START_TIME, 'MM')='09' then 'm9' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='09' then SIGNUP_NUMBERS else '0' end) m9, 
count(case when TO_CHAR(s.START_TIME, 'MM')='10' then 'm10' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='10' then SIGNUP_NUMBERS else '0' end) m10, 
count(case when TO_CHAR(s.START_TIME, 'MM')='11' then 'm11' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='11' then SIGNUP_NUMBERS else '0' end) m11, 
count(case when TO_CHAR(s.START_TIME, 'MM')='12' then 'm12' end) || '/' || sum(case when TO_CHAR(s.START_TIME, 'MM')='12' then SIGNUP_NUMBERS else '0' end) m12,
count(s.id) ||'/'||sum(SIGNUP_NUMBERS) nums
FROM SH_TRAIN s 
where to_char(s.start_time,'YYYY')='2018' and s.is_publish='1' and s.regioncode like '6523%'
group by s.sponsor,s.regioncode ) train left join t_s_territory terri on train.regioncode = terri.id

最終查詢結果以下,其中月份中顯示兩個字段 「培訓次數/參加培訓的人數」:spa

二、decode方法使用:3d

select count(1) as cnt,decode(max(ssex),0,'','') as sex from staffmgtview where orgid = '40288016615433bf0161543f9899000d' group by ssex order by ssex asc;

輸出結果:code

三、case when根據生日將全部員工信息分年齡段彙總輸出blog

select case 
  when sbirthday is null then '年齡不詳'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) < 20 then '20歲如下'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 20 and 29 then '20到29歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 30 and 39 then '30到39歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 40 and 49 then '40到49歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 50 and 59 then '50到59歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12)>=60 then '60歲以上' end as age_level, 
count(1) as cnt  
from staffmgtview where orgid = '40288016615433bf0161543f9899000d' group by case 
  when sbirthday is null then '年齡不詳'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) < 20 then '20歲如下'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 20 and 29 then '20到29歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 30 and 39 then '30到39歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 40 and 49 then '40到49歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 50 and 59 then '50到59歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12)>=60 then '60歲以上' end;

輸出結果:
排序

四、聯合查詢後分組輸出,根據字典表動態分組:it

select count(1) as cnt,
max(dic.typename) as position
  from staffmgtview left join 
  (SELECT tstype.typecode as typecode, tstype.typename as typename FROM t_s_type tstype
WHERE tstype.typegroupid = (SELECT id FROM t_s_Typegroup gro WHERE gro.typegroupcode = 'POST')) dic on dic.typecode = staffmgtview.SPOST
   where orgid = '40288016615433bf0161543f9899000d' group by spost order by spost asc;

輸出結果:
io

 

五、一年的十二個月分組統計class

1)橫着顯示:date

SELECT 
sum(case when TO_CHAR(s.create_date, 'MM')='01' then '1' else '0' end) m1,
sum(case when TO_CHAR(s.create_date, 'MM')='02' then '1' else '0' end) m2, 
sum(case when TO_CHAR(s.create_date, 'MM')='03' then '1' else '0' end) m3,
sum(case when TO_CHAR(s.create_date, 'MM')='04' then '1' else '0' end) m4,
sum(case when TO_CHAR(s.create_date, 'MM')='05' then '1' else '0' end) m5, 
sum(case when TO_CHAR(s.create_date, 'MM')='06' then '1' else '0' end) m6, 
sum(case when TO_CHAR(s.create_date, 'MM')='07' then '1' else '0' end) m7, 
sum(case when TO_CHAR(s.create_date, 'MM')='08' then '1' else '0' end) m8, 
sum(case when TO_CHAR(s.create_date, 'MM')='09' then '1' else '0' end) m9, 
sum(case when TO_CHAR(s.create_date, 'MM')='10' then '1' else '0' end) m10, 
sum(case when TO_CHAR(s.create_date, 'MM')='11' then '1' else '0' end) m11, 
sum(case when TO_CHAR(s.create_date, 'MM')='12' then '1' else '0' end) m12,
sum('1') nums
FROM oldcheckinview s 
where to_char(s.create_date,'YYYY')='2018' and org_id = '40288002623167ae0162319816b30131' group by TO_CHAR(s.create_date, 'MM');

結果:

2)豎着顯示:

select * from (
select 
sum(case when TO_CHAR(s.create_date, 'MM')='01' then '1' else '0' end) m1,
sum(case when TO_CHAR(s.create_date, 'MM')='02' then '1' else '0' end) m2, 
sum(case when TO_CHAR(s.create_date, 'MM')='03' then '1' else '0' end) m3,
sum(case when TO_CHAR(s.create_date, 'MM')='04' then '1' else '0' end) m4,
sum(case when TO_CHAR(s.create_date, 'MM')='05' then '1' else '0' end) m5, 
sum(case when TO_CHAR(s.create_date, 'MM')='06' then '1' else '0' end) m6, 
sum(case when TO_CHAR(s.create_date, 'MM')='07' then '1' else '0' end) m7, 
sum(case when TO_CHAR(s.create_date, 'MM')='08' then '1' else '0' end) m8, 
sum(case when TO_CHAR(s.create_date, 'MM')='09' then '1' else '0' end) m9, 
sum(case when TO_CHAR(s.create_date, 'MM')='10' then '1' else '0' end) m10, 
sum(case when TO_CHAR(s.create_date, 'MM')='11' then '1' else '0' end) m11, 
sum(case when TO_CHAR(s.create_date, 'MM')='12' then '1' else '0' end) m12,
sum('1') nums
from oldcheckinview s where org_id = '40288002623167ae0162319816b30131' and TO_CHAR(s.create_date, 'YYYY')='2018' )
unpivot (sum2015 for years in (m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,nums) ); 

效果如圖:

六、分組統計以後排序,須要巧用引入虛擬表進行排序

select a.age_level,decode(b.cnt,null,0,b.cnt) as cnt from (
select '20歲如下' age_level,1 age_order from dual
union all 
select '20到29歲' age_level,2 age_order from dual
union all 
select '30到39歲' age_level,3 age_order from dual
union all 
select '40到49歲' age_level,4 age_order from dual
union all 
select '50到59歲' age_level,5 age_order from dual
union all 
select '60歲以上' age_level,6 age_order from dual) a

left join 
(select case 
  when sbirthday is null then '年齡不詳'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) < 20 then '20歲如下'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 20 and 29 then '20到29歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 30 and 39 then '30到39歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 40 and 49 then '40到49歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 50 and 59 then '50到59歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12)>=60 then '60歲以上' end as age_level, 
count(1) as cnt  
from staffmgtview where orgid = '40288002616e0e9801616ef640460271' group by case 
  when sbirthday is null then '年齡不詳'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) < 20 then '20歲如下'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 20 and 29 then '20到29歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 30 and 39 then '30到39歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 40 and 49 then '40到49歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12) between 50 and 59 then '50到59歲'
  when floor(MONTHS_BETWEEN(sysdate,sbirthday)/12)>=60 then '60歲以上' end ) b on a.age_level = b.age_level order by a.age_order;
相關文章
相關標籤/搜索