语法:row_number() over (partition by 字段a order by 计算项b desc ) rank
--这里rank是别名
partition by:类似hive的建表,分区的意思;
order by :排序,默认是升序,加desc降序;
这里按字段a分区,对计算项b进行降序排序
例子:
over()中也可以什么都不写 就是多加一列输出行数
hive> select * from a;
OK
95001
95002
95003
95004
95005
95006
95007
95008
95009
95010
95011
hive>select a_1,row_number() over() from a;
95022 1
95021 2
95020 3
95019 4
95018 5
95017 6
95016 7
95015 8
95014 9
95013 10
95012 11
95011 12
数据显示为
empid deptid salary
----------- ----------- ---------------------------------------
1 10 5500.00
2 10 4500.00
3 20 1900.00
4 20 4800.00
5 40 6500.00
6 40 14500.00
7 40 44500.00
8 50 6500.00
9 50 7500.00
需求:根据部门分组,显示每个部门的工资等级
预期结果:
empid deptid salary rank
----------- ----------- --------------------------------------- --------------------
1 10 5500.00 1
2 10 4500.00 2
4 20 4800.00 1
3 20 1900.00 2
7 40 44500.00 1
6 40 14500.00 2
5 40 6500.00 3
9 50 7500.00 1
8 50 6500.00 2
SQL脚本:
SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) rank FROM employee
阅读(7154) | 评论(0) | 转发(0) |