Oracle 分组统计,抽取每组前十

/**
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;

原文地址:https://www.cnblogs.com/fooxer/p/9184242.html