标签:art -- 相同 number info 函数 编号 seconds strong
row_number() OVER (PARTITION BY COL1 ORDER BY COL2)
表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)。
-- rn_id 按照性别分组,按照id排序
select sex,name,id,
row_number() over(partition by sex order by id desc) as rn_id
from student_grouping;
-- 运行结果
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 3.07 sec HDFS Read: 9758 HDFS Write: 298 SUCCESS
Total MapReduce CPU Time Spent: 3 seconds 70 msec
OK
boy name6 6 1 boy name5 5 2 boy name4 4 3 boy name2 2 4 boy name1 1 5 girl name8 8 1 girl name7 7 2 girl name3 3 3
一个查询语句,有两个row_number(),并且两个分组一样,但排序不一样,查询出来的数据会跟着哪个走?
-- rn_id 按照性别分组,按照id排序 -- rn_age 按照性别分组,按照age 排序 select sex,name,id,age, row_number() over(partition by sex order by id desc) as rn_id, row_number() over(partition by sex order by age desc) as rn_age from student_grouping;
查询结果:
select sex,name,id,age, rank() over(partition by sex order by age desc) as rk1, dense_rank() over(partition by sex order by age desc) as rk2, row_number() over(partition by sex order by age desc) as rn1 from student_grouping;
查询结果
标签:art -- 相同 number info 函数 编号 seconds strong
原文地址:https://www.cnblogs.com/zhuziz/p/12382192.html