工作中用到了几个hive开窗函数,便想把hive开窗函数系统梳理一遍。

开窗函数

  1. 普通的聚合函数聚合的行集是组,开窗函数聚合的行集是窗口。因此,普通的聚合函数每组(Group by)只返回一个值,而开窗函数则可为窗口中的每行都返回一个值。简单理解,就是对查询的结果多出一列,这一列可以是聚合值,也可以是排序值。开窗函数一般分为两类,聚合开窗函数和排序开窗函数。

测试数据

  1. -- 建表
  2. create table student_scores(
  3. id int,
  4. studentId int,
  5. language int,
  6. math int,
  7. english int,
  8. classId string,
  9. departmentId string
  10. );
  11. -- 写入数据
  12. insert into table student_scores values
  13. (1,111,68,69,90,'class1','department1'),
  14. (2,112,73,80,96,'class1','department1'),
  15. (3,113,90,74,75,'class1','department1'),
  16. (4,114,89,94,93,'class1','department1'),
  17. (5,115,99,93,89,'class1','department1'),
  18. (6,121,96,74,79,'class2','department1'),
  19. (7,122,89,86,85,'class2','department1'),
  20. (8,123,70,78,61,'class2','department1'),
  21. (9,124,76,70,76,'class2','department1'),
  22. (10,211,89,93,60,'class1','department2'),
  23. (11,212,76,83,75,'class1','department2'),
  24. (12,213,71,94,90,'class1','department2'),
  25. (13,214,94,94,66,'class1','department2'),
  26. (14,215,84,82,73,'class1','department2'),
  27. (15,216,85,74,93,'class1','department2'),
  28. (16,221,77,99,61,'class2','department2'),
  29. (17,222,80,78,96,'class2','department2'),
  30. (18,223,79,74,96,'class2','department2'),
  31. (19,224,75,80,78,'class2','department2'),
  32. (20,225,82,85,63,'class2','department2');

聚合开窗函数

count开窗函数

  1. -- count 开窗函数
  2. select studentId,math,departmentId,classId,
  3. -- 以符合条件的所有行作为窗口
  4. count(math) over() as count1,
  5. -- 以按classId分组的所有行作为窗口
  6. count(math) over(partition by classId) as count2,
  7. -- 以按classId分组、按math排序的所有行作为窗口
  8. count(math) over(partition by classId order by math) as count3,
  9. -- 以按classId分组、按math排序、按 当前行+往前1行+往后2行的行作为窗口
  10. count(math) over(partition by classId order by math rows between 1 preceding and 2 following) as count4
  11. from student_scores where departmentId='department1';
  12. 结果
  13. studentid math departmentid classid count1 count2 count3 count4
  14. 111 69 department1 class1 9 5 1 3
  15. 113 74 department1 class1 9 5 2 4
  16. 112 80 department1 class1 9 5 3 4
  17. 115 93 department1 class1 9 5 4 3
  18. 114 94 department1 class1 9 5 5 2
  19. 124 70 department1 class2 9 4 1 3
  20. 121 74 department1 class2 9 4 2 4
  21. 123 78 department1 class2 9 4 3 3
  22. 122 86 department1 class2 9 4 4 2
  23. 结果解释:
  24. studentid=115,count1为所有的行数9,count2为分区class1中的行数5,count3为分区class1math值<=93的行数4,
  25. count4为分区class1math值向前+1行向后+2行(实际只有1行)的总行数3

sum开窗函数

  1. -- sum开窗函数
  2. select studentId,math,departmentId,classId,
  3. -- 以符合条件的所有行作为窗口
  4. sum(math) over() as sum1,
  5. -- 以按classId分组的所有行作为窗口
  6. sum(math) over(partition by classId) as sum2,
  7. -- 以按classId分组、按math排序后、按到当前行(含当前行)的所有行作为窗口
  8. sum(math) over(partition by classId order by math) as sum3,
  9. -- 以按classId分组、按math排序后、按当前行+往前1行+往后2行的行作为窗口
  10. sum(math) over(partition by classId order by math rows between 1 preceding and 2 following) as sum4
  11. from student_scores where departmentId='department1';
  12. 结果
  13. studentid math departmentid classid sum1 sum2 sum3 sum4
  14. 111 69 department1 class1 718 410 69 223
  15. 113 74 department1 class1 718 410 143 316
  16. 112 80 department1 class1 718 410 223 341
  17. 115 93 department1 class1 718 410 316 267
  18. 114 94 department1 class1 718 410 410 187
  19. 124 70 department1 class2 718 308 70 222
  20. 121 74 department1 class2 718 308 144 308
  21. 123 78 department1 class2 718 308 222 238
  22. 122 86 department1 class2 718 308 308 164
  23. 结果解释:
  24. count开窗函数

min开窗函数

  1. -- min 开窗函数
  2. select studentId,math,departmentId,classId,
  3. -- 以符合条件的所有行作为窗口
  4. min(math) over() as min1,
  5. -- 以按classId分组的所有行作为窗口
  6. min(math) over(partition by classId) as min2,
  7. -- 以按classId分组、按math排序后、按到当前行(含当前行)的所有行作为窗口
  8. min(math) over(partition by classId order by math) as min3,
  9. -- 以按classId分组、按math排序后、按当前行+往前1行+往后2行的行作为窗口
  10. min(math) over(partition by classId order by math rows between 1 preceding and 2 following) as min4
  11. from student_scores where departmentId='department1';
  12. 结果
  13. studentid math departmentid classid min1 min2 min3 min4
  14. 111 69 department1 class1 69 69 69 69
  15. 113 74 department1 class1 69 69 69 69
  16. 112 80 department1 class1 69 69 69 74
  17. 115 93 department1 class1 69 69 69 80
  18. 114 94 department1 class1 69 69 69 93
  19. 124 70 department1 class2 69 70 70 70
  20. 121 74 department1 class2 69 70 70 70
  21. 123 78 department1 class2 69 70 70 74
  22. 122 86 department1 class2 69 70 70 78
  23. 结果解释:
  24. count开窗函数

max开窗函数

  1. -- max 开窗函数
  2. select studentId,math,departmentId,classId,
  3. -- 以符合条件的所有行作为窗口
  4. max(math) over() as max1,
  5. -- 以按classId分组的所有行作为窗口
  6. max(math) over(partition by classId) as max2,
  7. -- 以按classId分组、按math排序后、按到当前行(含当前行)的所有行作为窗口
  8. max(math) over(partition by classId order by math) as max3,
  9. -- 以按classId分组、按math排序后、按当前行+往前1行+往后2行的行作为窗口
  10. max(math) over(partition by classId order by math rows between 1 preceding and 2 following) as max4
  11. from student_scores where departmentId='department1';
  12. 结果
  13. studentid math departmentid classid max1 max2 max3 max4
  14. 111 69 department1 class1 94 94 69 80
  15. 113 74 department1 class1 94 94 74 93
  16. 112 80 department1 class1 94 94 80 94
  17. 115 93 department1 class1 94 94 93 94
  18. 114 94 department1 class1 94 94 94 94
  19. 124 70 department1 class2 94 86 70 78
  20. 121 74 department1 class2 94 86 74 86
  21. 123 78 department1 class2 94 86 78 86
  22. 122 86 department1 class2 94 86 86 86
  23. 结果解释:
  24. count开窗函数

avg开窗函数

  1. -- avg 开窗函数
  2. select studentId,math,departmentId,classId,
  3. -- 以符合条件的所有行作为窗口
  4. avg(math) over() as avg1,
  5. -- 以按classId分组的所有行作为窗口
  6. avg(math) over(partition by classId) as avg2,
  7. -- 以按classId分组、按math排序后、按到当前行(含当前行)的所有行作为窗口
  8. avg(math) over(partition by classId order by math) as avg3,
  9. -- 以按classId分组、按math排序后、按当前行+往前1行+往后2行的行作为窗口
  10. avg(math) over(partition by classId order by math rows between 1 preceding and 2 following) as avg4
  11. from student_scores where departmentId='department1';
  12. 结果
  13. studentid math departmentid classid avg1 avg2 avg3 avg4
  14. 111 69 department1 class1 79.77777777777777 82.0 69.0 74.33333333333333
  15. 113 74 department1 class1 79.77777777777777 82.0 71.5 79.0
  16. 112 80 department1 class1 79.77777777777777 82.0 74.33333333333333 85.25
  17. 115 93 department1 class1 79.77777777777777 82.0 79.0 89.0
  18. 114 94 department1 class1 79.77777777777777 82.0 82.0 93.5
  19. 124 70 department1 class2 79.77777777777777 77.0 70.0 74.0
  20. 121 74 department1 class2 79.77777777777777 77.0 72.0 77.0
  21. 123 78 department1 class2 79.77777777777777 77.0 74.0 79.33333333333333
  22. 122 86 department1 class2 79.77777777777777 77.0 77.0 82.0
  23. 结果解释:
  24. count开窗函数

first_value开窗函数

返回分区中的第一个值。

  1. -- first_value 开窗函数
  2. select studentId,math,departmentId,classId,
  3. -- 以符合条件的所有行作为窗口
  4. first_value(math) over() as first_value1,
  5. -- 以按classId分组的所有行作为窗口
  6. first_value(math) over(partition by classId) as first_value2,
  7. -- 以按classId分组、按math排序后、按到当前行(含当前行)的所有行作为窗口
  8. first_value(math) over(partition by classId order by math) as first_value3,
  9. -- 以按classId分组、按math排序后、按当前行+往前1行+往后2行的行作为窗口
  10. first_value(math) over(partition by classId order by math rows between 1 preceding and 2 following) as first_value4
  11. from student_scores where departmentId='department1';
  12. 结果
  13. studentid math departmentid classid first_value1 first_value2 first_value3 first_value4
  14. 111 69 department1 class1 69 69 69 69
  15. 113 74 department1 class1 69 69 69 69
  16. 112 80 department1 class1 69 69 69 74
  17. 115 93 department1 class1 69 69 69 80
  18. 114 94 department1 class1 69 69 69 93
  19. 124 70 department1 class2 69 74 70 70
  20. 121 74 department1 class2 69 74 70 70
  21. 123 78 department1 class2 69 74 70 74
  22. 122 86 department1 class2 69 74 70 78
  23. 结果解释:
  24. studentid=124 first_value1:第一个值是69,first_value2:classId=class1分区 math的第一个值是69

last_value开窗函数

返回分区中的第一个值。

  1. -- last_value 开窗函数
  2. select studentId,math,departmentId,classId,
  3. -- 以符合条件的所有行作为窗口
  4. last_value(math) over() as last_value1,
  5. -- 以按classId分组的所有行作为窗口
  6. last_value(math) over(partition by classId) as last_value2,
  7. -- 以按classId分组、按math排序后、按到当前行(含当前行)的所有行作为窗口
  8. last_value(math) over(partition by classId order by math) as last_value3,
  9. -- 以按classId分组、按math排序后、按当前行+往前1行+往后2行的行作为窗口
  10. last_value(math) over(partition by classId order by math rows between 1 preceding and 2 following) as last_value4
  11. from student_scores where departmentId='department1';
  12. 结果
  13. studentid math departmentid classid last_value1 last_value2 last_value3 last_value4
  14. 111 69 department1 class1 70 93 69 80
  15. 113 74 department1 class1 70 93 74 93
  16. 112 80 department1 class1 70 93 80 94
  17. 115 93 department1 class1 70 93 93 94
  18. 114 94 department1 class1 70 93 94 94
  19. 124 70 department1 class2 70 70 70 78
  20. 121 74 department1 class2 70 70 74 86
  21. 123 78 department1 class2 70 70 78 86
  22. 122 86 department1 class2 70 70 86 86

lag开窗函数

lag(col,n,default) 用于统计窗口内往上第n个值。
col:列名
n:往上第n行
default:往上第n行为NULL时候,取默认值,不指定则取NULL

  1. -- lag 开窗函数
  2. select studentId,math,departmentId,classId,
  3. --窗口内 往上取第二个 取不到时赋默认值60
  4. lag(math,2,60) over(partition by classId order by math) as lag1,
  5. --窗口内 往上取第二个 取不到时赋默认值NULL
  6. lag(math,2) over(partition by classId order by math) as lag2
  7. from student_scores where departmentId='department1';
  8. 结果
  9. studentid math departmentid classid lag1 lag2
  10. 111 69 department1 class1 60 NULL
  11. 113 74 department1 class1 60 NULL
  12. 112 80 department1 class1 69 69
  13. 115 93 department1 class1 74 74
  14. 114 94 department1 class1 80 80
  15. 124 70 department1 class2 60 NULL
  16. 121 74 department1 class2 60 NULL
  17. 123 78 department1 class2 70 70
  18. 122 86 department1 class2 74 74
  19. 结果解释:
  20. 3 lag1:窗口内(69 74 80) 当前行80 向上取第二个值为69
  21. 倒数第3 lag2:窗口内(70 74) 当前行74 向上取第二个值为NULL

lead开窗函数

lead(col,n,default) 用于统计窗口内往下第n个值。
col:列名
n:往下第n行
default:往下第n行为NULL时候,取默认值,不指定则取NULL

  1. -- lead开窗函数
  2. select studentId,math,departmentId,classId,
  3. --窗口内 往下取第二个 取不到时赋默认值60
  4. lead(math,2,60) over(partition by classId order by math) as lead1,
  5. --窗口内 往下取第二个 取不到时赋默认值NULL
  6. lead(math,2) over(partition by classId order by math) as lead2
  7. from student_scores where departmentId='department1';
  8. 结果
  9. studentid math departmentid classid lead1 lead2
  10. 111 69 department1 class1 80 80
  11. 113 74 department1 class1 93 93
  12. 112 80 department1 class1 94 94
  13. 115 93 department1 class1 60 NULL
  14. 114 94 department1 class1 60 NULL
  15. 124 70 department1 class2 78 78
  16. 121 74 department1 class2 86 86
  17. 123 78 department1 class2 60 NULL
  18. 122 86 department1 class2 60 NULL
  19. 结果解释:
  20. 4lead1 窗口内向下第二个值为空,赋值60

cume_dist开窗函数

计算某个窗口或分区中某个值的累积分布。假定升序排序,则使用以下公式确定累积分布:
小于等于当前值x的行数 / 窗口或partition分区内的总行数。其中,x 等于 order by 子句中指定的列的当前行中的值。

  1. -- cume_dist 开窗函数
  2. select studentId,math,departmentId,classId,
  3. -- 统计小于等于当前分数的人数占总人数的比例
  4. cume_dist() over(order by math) as cume_dist1,
  5. -- 统计大于等于当前分数的人数占总人数的比例
  6. cume_dist() over(order by math desc) as cume_dist2,
  7. -- 统计分区内小于等于当前分数的人数占总人数的比例
  8. cume_dist() over(partition by classId order by math) as cume_dist3
  9. from student_scores where departmentId='department1';
  10. 结果
  11. studentid math departmentid classid cume_dist1 cume_dist2 cume_dist3
  12. 111 69 department1 class1 0.1111111111111111 1.0 0.2
  13. 113 74 department1 class1 0.4444444444444444 0.7777777777777778 0.4
  14. 112 80 department1 class1 0.6666666666666666 0.4444444444444444 0.6
  15. 115 93 department1 class1 0.8888888888888888 0.2222222222222222 0.8
  16. 114 94 department1 class1 1.0 0.1111111111111111 1.0
  17. 124 70 department1 class2 0.2222222222222222 0.8888888888888888 0.25
  18. 121 74 department1 class2 0.4444444444444444 0.7777777777777778 0.5
  19. 123 78 department1 class2 0.5555555555555556 0.5555555555555556 0.75
  20. 122 86 department1 class2 0.7777777777777778 0.3333333333333333 1.0
  21. 结果解释:
  22. 第三行:
  23. cume_dist1=小于等于80的人数为6/总人数9=0.6666666666666666
  24. cume_dist2=大于等于80的人数为4/总人数9=0.4444444444444444
  25. cume_dist3=分区内小于等于80的人数为3/分区内总人数5=0.6

排序开窗函数

rank开窗函数

rank 开窗函数基于 over 子句中的 order by 确定一组值中一个值的排名。如果存在partition by ,则为每个分区组中的每个值排名。排名可能不是连续的。例如,如果两个行的排名为 1,则下一个排名为 3。

  1. -- rank 开窗函数
  2. select *,
  3. -- 对全部学生按数学分数排序
  4. rank() over(order by math) as rank1,
  5. -- 对院系 按数学分数排序
  6. rank() over(partition by departmentId order by math) as rank2,
  7. -- 对每个院系每个班级 按数学分数排序
  8. rank() over(partition by departmentId,classId order by math) as rank3
  9. from student_scores;
  10. 结果
  11. id studentid language math english classid departmentid rank1 rank2 rank3
  12. 1 111 68 69 90 class1 department1 1 1 1
  13. 3 113 90 74 75 class1 department1 3 3 2
  14. 2 112 73 80 96 class1 department1 9 6 3
  15. 5 115 99 93 89 class1 department1 15 8 4
  16. 4 114 89 94 93 class1 department1 17 9 5
  17. 9 124 76 70 76 class2 department1 2 2 1
  18. 6 121 96 74 79 class2 department1 3 3 2
  19. 8 123 70 78 61 class2 department1 7 5 3
  20. 7 122 89 86 85 class2 department1 14 7 4
  21. 15 216 85 74 93 class1 department2 3 1 1
  22. 14 215 84 82 73 class1 department2 11 5 2
  23. 11 212 76 83 75 class1 department2 12 6 3
  24. 10 211 89 93 60 class1 department2 15 8 4
  25. 12 213 71 94 90 class1 department2 17 9 5
  26. 13 214 94 94 66 class1 department2 17 9 5
  27. 18 223 79 74 96 class2 department2 3 1 1
  28. 17 222 80 78 96 class2 department2 7 3 2
  29. 19 224 75 80 78 class2 department2 9 4 3
  30. 20 225 82 85 63 class2 department2 13 7 4
  31. 16 221 77 99 61 class2 department2 20 11 5

dense_rank开窗函数

dense_rank与rank有一点不同,当排名一样的时候,接下来的行是连续的。如两个行的排名为 1,则下一个排名为 2。

  1. -- dense_rank 开窗函数
  2. select *,
  3. -- 对全部学生按数学分数排序
  4. dense_rank() over(order by math) as dense_rank1,
  5. -- 对院系 按数学分数排序
  6. dense_rank() over(partition by departmentId order by math) as dense_rank2,
  7. -- 对每个院系每个班级 按数学分数排序
  8. dense_rank() over(partition by departmentId,classId order by math) as dense_rank3
  9. from student_scores;
  10. 结果:
  11. id studentid language math english classid departmentid dense_rank1 dense_rank2 dense_rank3
  12. 1 111 68 69 90 class1 department1 1 1 1
  13. 3 113 90 74 75 class1 department1 3 3 2
  14. 2 112 73 80 96 class1 department1 5 5 3
  15. 5 115 99 93 89 class1 department1 10 7 4
  16. 4 114 89 94 93 class1 department1 11 8 5
  17. 9 124 76 70 76 class2 department1 2 2 1
  18. 6 121 96 74 79 class2 department1 3 3 2
  19. 8 123 70 78 61 class2 department1 4 4 3
  20. 7 122 89 86 85 class2 department1 9 6 4
  21. 15 216 85 74 93 class1 department2 3 1 1
  22. 14 215 84 82 73 class1 department2 6 4 2
  23. 11 212 76 83 75 class1 department2 7 5 3
  24. 10 211 89 93 60 class1 department2 10 7 4
  25. 12 213 71 94 90 class1 department2 11 8 5
  26. 13 214 94 94 66 class1 department2 11 8 5
  27. 18 223 79 74 96 class2 department2 3 1 1
  28. 17 222 80 78 96 class2 department2 4 2 2
  29. 19 224 75 80 78 class2 department2 5 3 3
  30. 20 225 82 85 63 class2 department2 8 6 4
  31. 16 221 77 99 61 class2 department2 12 9 5

ntile开窗函数

将分区中已排序的行划分为大小尽可能相等的指定数量的排名的组,并返回给定行所在的组的排名。

  1. -- ntile 开窗函数
  2. select *,
  3. -- 对分区内的数据分成两组
  4. ntile(2) over(partition by departmentid order by math) as ntile1,
  5. -- 对分区内的数据分成三组
  6. ntile(3) over(partition by departmentid order by math) as ntile2
  7. from student_scores;
  8. 结果
  9. id studentid language math english classid departmentid ntile1 ntile2
  10. 1 111 68 69 90 class1 department1 1 1
  11. 9 124 76 70 76 class2 department1 1 1
  12. 6 121 96 74 79 class2 department1 1 1
  13. 3 113 90 74 75 class1 department1 1 2
  14. 8 123 70 78 61 class2 department1 1 2
  15. 2 112 73 80 96 class1 department1 2 2
  16. 7 122 89 86 85 class2 department1 2 3
  17. 5 115 99 93 89 class1 department1 2 3
  18. 4 114 89 94 93 class1 department1 2 3
  19. 18 223 79 74 96 class2 department2 1 1
  20. 15 216 85 74 93 class1 department2 1 1
  21. 17 222 80 78 96 class2 department2 1 1
  22. 19 224 75 80 78 class2 department2 1 1
  23. 14 215 84 82 73 class1 department2 1 2
  24. 11 212 76 83 75 class1 department2 1 2
  25. 20 225 82 85 63 class2 department2 2 2
  26. 10 211 89 93 60 class1 department2 2 2
  27. 12 213 71 94 90 class1 department2 2 3
  28. 13 214 94 94 66 class1 department2 2 3
  29. 16 221 77 99 61 class2 department2 2 3
  30. 结果解释:
  31. 8
  32. ntile1:对分区的数据均匀分成2组后,当前行的组排名为2
  33. ntile2:对分区的数据均匀分成3组后,当前行的组排名为3

row_number开窗函数

从1开始对分区内的数据排序。

  1. -- row_number 开窗函数
  2. select studentid,departmentid,classid,math,
  3. -- 对分区departmentid,classid内的数据按math排序
  4. row_number() over(partition by departmentid,classid order by math) as row_number
  5. from student_scores;
  6. 结果
  7. studentid departmentid classid math row_number
  8. 111 department1 class1 69 1
  9. 113 department1 class1 74 2
  10. 112 department1 class1 80 3
  11. 115 department1 class1 93 4
  12. 114 department1 class1 94 5
  13. 124 department1 class2 70 1
  14. 121 department1 class2 74 2
  15. 123 department1 class2 78 3
  16. 122 department1 class2 86 4
  17. 216 department2 class1 74 1
  18. 215 department2 class1 82 2
  19. 212 department2 class1 83 3
  20. 211 department2 class1 93 4
  21. 213 department2 class1 94 5
  22. 214 department2 class1 94 6
  23. 223 department2 class2 74 1
  24. 222 department2 class2 78 2
  25. 224 department2 class2 80 3
  26. 225 department2 class2 85 4
  27. 221 department2 class2 99 5
  28. 结果解释:
  29. 同一分区,相同值,不同序。如studentid=213 studentid=214 值都为94 排序为5,6

percent_rank开窗函数

计算给定行的百分比排名。可以用来计算超过了百分之多少的人。如360小助手开机速度超过了百分之多少的人。
(当前行的rank值-1)/(分组内的总行数-1)

  1. -- percent_rank 开窗函数
  2. select studentid,departmentid,classid,math,
  3. row_number() over(partition by departmentid,classid order by math) as row_number,
  4. percent_rank() over(partition by departmentid,classid order by math) as percent_rank
  5. from student_scores;
  6. 结果
  7. studentid departmentid classid math row_number percent_rank
  8. 111 department1 class1 69 1 0.0
  9. 113 department1 class1 74 2 0.25
  10. 112 department1 class1 80 3 0.5
  11. 115 department1 class1 93 4 0.75
  12. 114 department1 class1 94 5 1.0
  13. 124 department1 class2 70 1 0.0
  14. 121 department1 class2 74 2 0.3333333333333333
  15. 123 department1 class2 78 3 0.6666666666666666
  16. 122 department1 class2 86 4 1.0
  17. 216 department2 class1 74 1 0.0
  18. 215 department2 class1 82 2 0.2
  19. 212 department2 class1 83 3 0.4
  20. 211 department2 class1 93 4 0.6
  21. 213 department2 class1 94 5 0.8
  22. 214 department2 class1 94 6 0.8
  23. 223 department2 class2 74 1 0.0
  24. 222 department2 class2 78 2 0.25
  25. 224 department2 class2 80 3 0.5
  26. 225 department2 class2 85 4 0.75
  27. 221 department2 class2 99 5 1.0
  28. 结果解释:
  29. studentid=115,percent_rank=(4-1)/(5-1)=0.75
  30. studentid=123,percent_rank=(3-1)/(4-1)=0.6666666666666666