码迷,mamicode.com
首页 > 编程语言 > 详细

多层分组排序问题

时间:2017-11-04 16:19:10      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   png   创建表   ber   image   实现   pre   table   sel   

已知原表数据如下:

技术分享

要求实现如下效果:

技术分享

--创建表
create table tmp as
select 火腿肠 c1, 销售一部 c2,老王   c3, 1.5 c4 from dual union all
select 玉米   c1, 销售五部 c2,B老刘  c3, 1   c4 from dual union all
select 笔记本 c1, 销售一部 c2,老王   c3, 3   c4 from dual union all
select 笔记本 c1, 销售七部 c2,老张   c3, 3   c4 from dual union all
select 桌子   c1, 销售九部 c2,老孙   c3, 4   c4 from dual union all
select 火腿肠 c1, 销售五部 c2,老刘   c3, 1.5 c4 from dual union all
select 火腿肠 c1, 销售四部 c2,老赵   c3, 1.5 c4 from dual union all
select 玉米   c1, 销售四部 c2,老李   c3, 1   c4 from dual union all
select 笔记本 c1, 销售七部 c2,老张   c3, 3   c4 from dual union all
select 玉米   c1, 销售五部 c2,A老谭  c3, 1   c4 from dual union all
select 玉米   c1, 销售一部 c2,老柳   c3, 1   c4 from dual union all
select 玉米   c1, 销售一部 c2,老柳   c3, 1   c4 from dual union all
select 玉米   c1, 销售一部 c2,老王   c3, 1   c4 from dual;

--写法1:
select rn,c1,rn2,c2,rn3,c3
  from(select c1,c2,c3,
              dense_rank()over(order by sm desc) rn,
              dense_rank()over(partition by c1 order by sm2 desc) rn2,
              row_number()over(partition by c1,c2 order by sm3 desc) rn3
         from(select distinct t1.c1,t1.c2,t1.c3,
                     sum(t1.c4)over(partition by t1.c1) sm,
                     sum(t1.c4)over(partition by t1.c1,t1.c2) sm2,
                     sum(t1.c4)over(partition by t1.c1,t1.c2,t1.c3) sm3
                from tmp t1
             )
       )
where rn <= 3 and rn2 <= 3 and rn3 = 1
order by rn,rn2,rn3;
 
--写法2
select rn,c1,rn2,c2,rn3,c3
  from(select c1,c2,c3,
              dense_rank()over(order by sm desc) rn,
              dense_rank()over(partition by c1 order by sm2 desc) rn2,
              dense_rank()over(partition by c1,c2 order by sm3 desc) rn3
         from(select distinct t1.c1,t1.c2,t1.c3,
                     sum(t1.c4)over(partition by t1.c1) sm,
                     sum(t1.c4)over(partition by t1.c1,t1.c2) sm2,
                     sum(t1.c4)over(partition by t1.c1,t1.c2,t1.c3) sm3
                from tmp t1
             )
       )
where rn <= 3 and rn2 <= 3 and rn3 = 1
order by rn,rn2,rn3;

 

多层分组排序问题

标签:style   png   创建表   ber   image   实现   pre   table   sel   

原文地址:http://www.cnblogs.com/huangbiquan/p/7783312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!