标签:
Oracle的分析函数功能非常强大,工作这些年来经常用到。这次将平时经常使用到的分析函数整理出来,以备日后查看。我们拿案例来学习,这样理解起来更容易一些。
create table earnings -- 打工赚钱表 ( earnmonth varchar2(6), -- 打工月份 area varchar2(20), -- 打工地区 sno varchar2(10), -- 打工者编号 sname varchar2(20), -- 打工者姓名 times int, -- 本月打工次数 singleincome number(10,2), -- 每次赚多少钱 personincome number(10,2) -- 当月总收入 )
insert into earnings values('200912','北平','511601','大魁',11,30,11*30); insert into earnings values('200912','北平','511602','大凯',8,25,8*25); insert into earnings values('200912','北平','511603','小东',30,6.25,30*6.25); insert into earnings values('200912','北平','511604','大亮',16,8.25,16*8.25); insert into earnings values('200912','北平','511605','贱敬',30,11,30*11); insert into earnings values('200912','金陵','511301','小玉',15,12.25,15*12.25); insert into earnings values('200912','金陵','511302','小凡',27,16.67,27*16.67); insert into earnings values('200912','金陵','511303','小妮',7,33.33,7*33.33); insert into earnings values('200912','金陵','511304','小俐',0,18,0); insert into earnings values('200912','金陵','511305','雪儿',11,9.88,11*9.88); insert into earnings values('201001','北平','511601','大魁',0,30,0); insert into earnings values('201001','北平','511602','大凯',14,25,14*25); insert into earnings values('201001','北平','511603','小东',19,6.25,19*6.25); insert into earnings values('201001','北平','511604','大亮',7,8.25,7*8.25); insert into earnings values('201001','北平','511605','贱敬',21,11,21*11); insert into earnings values('201001','金陵','511301','小玉',6,12.25,6*12.25); insert into earnings values('201001','金陵','511302','小凡',17,16.67,17*16.67); insert into earnings values('201001','金陵','511303','小妮',27,33.33,27*33.33); insert into earnings values('201001','金陵','511304','小俐',16,18,16*18); insert into earnings values('201001','金陵','511305','雪儿',11,9.88,11*9.88); commit;
select * from earnings;查询结果如下
select earnmonth, area, sum(personincome) from earnings group by earnmonth,area;查询结果如下
select earnmonth, area, sum(personincome) from earnings group by rollup(earnmonth,area);查询结果如下
select earnmonth, area, sum(personincome) from earnings group by cube(earnmonth,area) order by earnmonth,area nulls last;查询结果如下
select decode(grouping(earnmonth),1,'所有月份',earnmonth) 月份, decode(grouping(area),1,'全部地区',area) 地区, sum(personincome) 总金额 from earnings group by cube(earnmonth,area) order by earnmonth,area nulls last;
查询结果如下
按照月份、地区,求打工收入排序
select earnmonth 月份,area 地区,sname 打工者, personincome 收入, rank() over (partition by earnmonth,area order by personincome desc) 排名 from earnings;查询结果如下
select earnmonth 月份,area 地区,sname 打工者, personincome 收入, dense_rank() over (partition by earnmonth,area order by personincome desc) 排名 from earnings;查询结果如下
select earnmonth 月份,area 地区,sname 打工者, personincome 收入, row_number() over (partition by earnmonth,area order by personincome desc) 排名 from earnings;查询结果如下
通过(8)(9)(10)发现rank,dense_rank,row_number的区别:
结果集中如果出现两个相同的数据,那么rank会进行跳跃式的排名,
比如两个第二,那么没有第三接下来就是第四;
但是dense_rank不会跳跃式的排名,两个第二接下来还是第三;
row_number最牛,即使两个数据相同,排名也不一样。
select earnmonth 月份,area 地区,sname 打工者, sum(personincome) over (partition by earnmonth,area order by personincome) 总收入 from earnings;查询结果如下
select distinct earnmonth 月份, area 地区, max(personincome) over(partition by earnmonth,area) 最高值, min(personincome) over(partition by earnmonth,area) 最低值, avg(personincome) over(partition by earnmonth,area) 平均值, sum(personincome) over(partition by earnmonth,area) 总额 from earnings;查询结果如下
select earnmonth 本月,sname 打工者, lag(decode(nvl(personincome,0),0,'没赚','赚了'),1,0) over(partition by sname order by earnmonth) 上月, lead(decode(nvl(personincome,0),0,'没赚','赚了'),1,0) over(partition by sname order by earnmonth) 下月 from earnings;查询结果如下
说明:Lag和Lead函数可以在一次查询中取出某个字段的前N行和后N行的数据(可以是其他字段的数据,比如根据字段甲查询上一行或下两行的字段乙)
语法如下:
lag(value_expression [,offset] [,default]) over ([query_partition_clase] order_by_clause);
lead(value_expression [,offset] [,default]) over ([query_partition_clase] order_by_clause);
其中:
value_expression:可以是一个字段或一个内建函数。
offset是正整数,默认为1,指往前或往后几点记录.因组内第一个条记录没有之前的行,最后一行没有之后的行,
default就是用于处理这样的信息,默认为空。
再讲讲所谓的开窗函数,依本人遇见,开窗函数就是 over([query_partition_clase] order_by_clause)。比如说,我采用sum求和,rank排序等等,但是我根据什么来呢?over提供一个窗口,可以根据什么什么分组,就用partition by,然后在组内根据什么什么进行内部排序,就用 order by。
标签:
原文地址:http://blog.csdn.net/jay_1989/article/details/52275173