1. 程式人生 > >Oracle 分組統計,抽取每組前十

Oracle 分組統計,抽取每組前十

order by 編號 用戶名 ldb In 行業 partition like ber

/**
2018年6月14日 潮州
ORACEL 統計2017年用電量,按行業分類抽取用電量前十
*/
select *
from (select t.yhbh 用戶編號,
t.yhmc 用戶名稱,
t.jldbh 計量點編號,
(select m.dmbmmc
from npmis_xt_dmbm m
where m.dmfl = ‘YDLXDM‘
and m.dmbm = t.ydlbdm) 用電類型,
(select m.dmbmmc
from npmis_xt_dmbm m
where m.dmbmbs = (select m.sjdmbmbs
from npmis_xt_dmbm m
where m.dmfl = ‘HYFLDM‘
and m.dmbm = t.hyfldm)) 上級行業分類,
(select m.dmbmmc
from npmis_xt_dmbm m
where m.dmfl = ‘HYFLDM‘
and m.dmbm = t.hyfldm) 行業分類,
sum(t.jfdl) 總計費電量,
row_number() over(partition by t.hyfldm order by sum(t.jfdl) desc) 本行業排名
from npmis_hs_jldxx t
where t.dfny like ‘2017%‘
and t.ydlbdm in (‘100‘, ‘200‘, ‘260‘, ‘300‘) /**用電類別為:大工業、非普通工業、非工業、商業*/
and t.jfdl <> 0 /**計費電量不為0*/
and t.yhztdm <> ‘2‘ /**用戶狀態不為銷戶*/
group by t.yhbh, t.yhmc, t.jldbh, t.ydlbdm, t.hyfldm
having sum(t.jfdl) > 500000
order by 總計費電量)
where 本行業排名 < 11;

Oracle 分組統計,抽取每組前十