当前位置:   article > 正文

OVER(PARTITION BY)使用方法_over partition by 取第二行

over partition by 取第二行

原文出处:https://www.cnblogs.com/lcngu/p/5335170.html

  • 简介

  开窗函数,Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行。

  开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化。

  下面的测试用例数据语句如下:

  1. create table T2_TEMP(
  2. NAME varchar2(10) primary key,
  3. CLASS varchar2(10),
  4. SROCE NUMBER
  5. )
  6. insert into T2_TEMP (NAME, CLASS, SROCE)
  7. values ('cfe', '2', 74);
  8. insert into T2_TEMP (NAME, CLASS, SROCE)
  9. values ('dss', '1', 95);
  10. insert into T2_TEMP (NAME, CLASS, SROCE)
  11. values ('ffd', '1', 95);
  12. insert into T2_TEMP (NAME, CLASS, SROCE)
  13. values ('fda', '1', 80);
  14. insert into T2_TEMP (NAME, CLASS, SROCE)
  15. values ('gds', '2', 92);
  16. insert into T2_TEMP (NAME, CLASS, SROCE)
  17. values ('gf', '3', 99);
  18. insert into T2_TEMP (NAME, CLASS, SROCE)
  19. values ('ddd', '3', 99);
  20. insert into T2_TEMP (NAME, CLASS, SROCE)
  21. values ('adf', '3', 45);
  22. insert into T2_TEMP (NAME, CLASS, SROCE)
  23. values ('asdf', '3', 55);
  24. insert into T2_TEMP (NAME, CLASS, SROCE)
  25. values ('3dd', '3', 78);

    1、over函数的写法:

  over(partition by class order by sroce) 按照sroce排序进行累计,order by是个默认的开窗函数,按照class分区。

  2、开窗的窗口范围:

  over(order by sroce range between 5 preceding and 5 following):窗口范围为当前行数据幅度减5加5后的范围内的。

  over(order by sroce rows between 5 preceding and 5 following):窗口范围为当前行前后各移动5行。

  3、与over()函数结合的函数的介绍

     (1)、查询每个班的第一名的成绩:如下 

1 SELECT * FROM (select t.name,t.class,t.sroce,rank() over(partition by t.class order by t.sroce desc) mm from T2_TEMP t) where mm = 1;

       结果为:

  1. 1 得到的结果是:
  2. 2 dss 1 95 1
  3. 3 ffd 1 95 1
  4. 4 gds 2 92 1
  5. 5 gf 3 99 1
  6. 6 ddd 3 99 1

 注意:在求第一名成绩的时候,不能用row_number(),因为如果同班有两个并列第一,row_number()只返回一个结果。

1 SELECT * FROM (select t.name,t.class,t.sroce,row_number() over(partition by t.class order by t.sroce desc) mm from T2_TEMP t) where mm = 1;

  结果为:

  1. dss 1        95        1 
  2. gfs 2        92        1
  3. ddd 3        99        1

  可以看出,本来第一名是两个人的并列,结果只显示了一个。

  (2)、rank()和dense_rank()可以将所有的都查找出来,rank可以将并列第一名的都查找出来;rank()和dense_rank()区别:rank()是跳跃排序,有两个第二名时接下来就是第四名。

  求班级成绩排名:

1 select t.name,t.class,t.sroce,rank() over(partition by t.class order by t.sroce desc) mm from T2_TEMP t;

  查询结果:

  1. dss        1        95        1
  2. ffd        1        95        1
  3. fda        1        80        3
  4. gds        2        92        1
  5. cfe        2        74        2
  6. gf         3        99        1
  7. ddd        3        99        1
  8. 3dd        3        78        3
  9. asdf       3        55        4
  10. adf        3        45        5

  dense_rank()l是连续排序,有两个第二名时仍然跟着第三名

1 select t.name,t.class,t.sroce,dense_rank() over(partition by t.class order by t.sroce desc) mm from T2_TEMP t;

  查询结果:

 

  1. dss        1        95        1
  2. ffd        1        95        1
  3. fda        1        80        2
  4. gds        2        92        1
  5. cfe        2        74        2
  6. gf         3        99        1
  7. ddd        3        99        1
  8. 3dd        3        78        2
  9. asdf       3        55        3
  10. adf        3        45        4

 

  3、sum()over()的使用

  根据班级进行分数求和

1 select t.name,t.class,t.sroce,sum(t.sroce) over(partition by t.class order by t.sroce desc) mm from T2_TEMP t;

 

  1. dss        1        95        190  --由于两个95都是第一名,所以累加时是两个第一名的相加
  2. ffd        1        95        190
  3. fda        1        80        270  --第一名加上第二名的
  4. gds        2        92        92
  5. cfe        2        74        166
  6. gf         3        99        198
  7. ddd        3        99        198
  8. 3dd        3        78        276
  9. asdf       3        55        331
  10. adf        3        45        376

 

  4、first_value() over()和last_value() over()的使用 

  1. 1 select t.name,t.class,t.sroce,first_value(t.sroce) over(partition by t.class order by t.sroce desc) mm from T2_TEMP t;
  2. 2 select t.name,t.class,t.sroce,last_value(t.sroce) over(partition by t.class order by t.sroce desc) mm from T2_TEMP t;

  分别求出第一个和最后一个成绩。

  5、sum() over()的使用

1 select t.name,t.class,t.sroce,sum(t.sroce) over(partition by t.class order by t.sroce desc) mm from T2_TEMP t;

  求出班级的总分。

  下面还有很多用法,就不一一列举了,简单介绍一下,和上面用法类似:

  count() over(partition by ... order by ...):求分组后的总数。
  max() over(partition by ... order by ...):求分组后的最大值。
  min() over(partition by ... order by ...):求分组后的最小值。
  avg() over(partition by ... order by ...):求分组后的平均值。
  lag() over(partition by ... order by ...):取出前n行数据。  

  lead() over(partition by ... order by ...):取出后n行数据。

  ratio_to_report() over(partition by ... order by ...):Ratio_to_report() 括号中就是分子,over() 括号中就是分母。

  percent_rank() over(partition by ... order by ...):

  6、over partition by与group by的区别:

  group by是对检索结果的保留行进行单纯分组,一般和聚合函数一起使用例如max、min、sum、avg、count等一块用。partition by虽然也具有分组功能,但同时也具有其他的高级功能。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/241061
推荐阅读
相关标签
  

闽ICP备14008679号