码迷,mamicode.com
首页 > 数据库 > 详细

Oracle 高级排序函数 和 高级分组函数

时间:2017-07-28 14:12:49      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:color   set   等级   _id   方式   order   学生   nec   group   

高级排序函数: 
[ 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 高级排序函数 和 高级分组函数

标签:color   set   等级   _id   方式   order   学生   nec   group   

原文地址:http://www.cnblogs.com/GotoJava/p/7249701.html

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