Oracle 高级排序函数 和 高级分组函数
Posted K____K
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle 高级排序函数 和 高级分组函数相关的知识,希望对你有一定的参考价值。
高级排序函数:
[ ROW_NUMBER()| RANK() | DENSE_RANK ] OVER (partition by xx order by xx)
1.row_number() 连续且递增的数字 1 2 3 4
row_number() over (partition by xx order by xx )
--学生表中按照所在专业分组,同专业内按成绩倒序排序,成绩相同则按学号正序排序,并给予组内等级
select row_number() over(partition by class_id order by score desc)rn,t.* from student2016 t
2.rank() 跳跃排序 若有相同数据则排名相同 然后跳跃排序 1 2 2 2 5
rank() over (partition by xx order by xx )
select rank() over(partition by class_id order by score desc)rn,t.* from student2016 t
3.dense_rank 若有相同数据则排名相同 然后递增排序
dense_rank over (partition by xx order by xx ) 1 2 2 2 3
select dense_rank() over(partition by class_id order by score desc)rn,t.* from student2016 t
----------------------------------------------------------------------------------------------------------------------------
高级分组函数
group by rollup(a,b,c)
select a,b,c,sum(d) from test group by rollup(a,b,c)
对rollup后面的列 按从右到左以少一列的方式进行分组直到所有列都去掉后的分组(也就是全表分组)
对于n个参数的 rollup,有n+1次分组
即按a,b,c,分组,union all a,b分组 union all a分组 union from test
----------------------------------------------------------------------------------
group by cube(a,b,c)
对n个参数,有2^n次分组
即按 ab,ac,a,bc,b,c最后对 全部分组
----------------------------------------------------------------------------------
group by grouping sets(a,b)
即只列出 对 a分组后,和对 b分组的结果集
-- 创建销售表 create table sales_tab( year_id number not null, month_id number not null, day_id number not null, sales_value number(10,2) not null ); -- 插入数据 insert into sales_tab select trunc(dbms_random.value(low=>2010,high=>2012)) as year_id, trunc(dbms_random.value(low=>1,high=>13)) as month_id, trunc(dbms_random.value(low=>1,high=>32)) as day_id, round(dbms_random.value(low=>1,high=>100)) as sales_value from dual connect by level <=1000; -- 查询 group by 后的数据 select sum(t.sales_value) from SALES_TAB t -- 1行 select t.year_id,t.month_id,t.day_id,sum(t.sales_value) sales from SALES_TAB t group by t.year_id,t.month_id,t.day_id order by t.year_id,t.month_id,t.day_id desc; -- 540行 select t.year_id,t.month_id,sum(t.sales_value) sales from SALES_TAB t group by t.year_id,t.month_id order by t.year_id,t.month_id desc; -- 24 行 select t.year_id,sum(t.sales_value) sales from SALES_TAB t group by t.year_id order by t.year_id desc; -- 2 行 -- 使用高级分组函数 -- group by rollup(a,b,c) select t.year_id,t.month_id,t.day_id,sum(t.sales_value) sales from SALES_TAB t group by rollup(t.year_id,t.month_id,t.day_id) order by t.year_id,t.month_id,t.day_id; -- 567 行 = 同上面 1+540+24+2 -- group by cube(a,b,c) select t.year_id,t.month_id,t.day_id,sum(t.sales_value) sales from SALES_TAB t group by cube(t.year_id,t.month_id,t.day_id) order by t.year_id,t.month_id,t.day_id; --group by grouping sets(a,b,c) select t.year_id,t.month_id,sum(t.sales_value) sales from SALES_TAB t group by cube(t.year_id,t.month_id) order by 1,2; -- 39 行 select t.year_id,t.month_id,sum(t.sales_value) sales from SALES_TAB t group by grouping sets(t.year_id,t.month_id) order by 1,2; -- 14 行
以上是关于Oracle 高级排序函数 和 高级分组函数的主要内容,如果未能解决你的问题,请参考以下文章
[转]详解Oracle高级分组函数(ROLLUP, CUBE, GROUPING SETS)