Hive实现分组排序或者分页
程序员文章站
2022-04-26 18:57:48
使用到的语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)
简单的说row_number()从1开始,为每一条分组记录返回...
使用到的语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)
简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再为降序以后的没条xlh记录返回一个序号。
表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)实例:
初始化数据:
create table employee (empid int ,deptid int ,salary decimal(10,2));
insert into employee values(1,10,5500.00);
insert into employee values(2,10,4500.00);
insert into employee values(3,20,1900.00);
insert into employee values(4,20,4800.00);
insert into employee values(5,40,6500.00);
insert into employee values(6,40,14500.00);
insert into employee values(7,40,44500.00);
insert into employee values(8,50,6500.00);
insert into employee values(9,50,7500.00);
数据显示为:
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
基于row_number函数也很容易实现分页:
select * from (select row_number() over (order by empid desc) as rnum ,TableName.* from TableName)t where rnum >= 1 and rnum <= 5;
简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再为降序以后的没条xlh记录返回一个序号。
表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)实例:
初始化数据:
create table employee (empid int ,deptid int ,salary decimal(10,2));
insert into employee values(1,10,5500.00);
insert into employee values(2,10,4500.00);
insert into employee values(3,20,1900.00);
insert into employee values(4,20,4800.00);
insert into employee values(5,40,6500.00);
insert into employee values(6,40,14500.00);
insert into employee values(7,40,44500.00);
insert into employee values(8,50,6500.00);
insert into employee values(9,50,7500.00);
数据显示为:
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
基于row_number函数也很容易实现分页:
select * from (select row_number() over (order by empid desc) as rnum ,TableName.* from TableName)t where rnum >= 1 and rnum <= 5;
推荐阅读
-
C#拼接SQL语句 用ROW_NUMBER实现的高效分页排序
-
SQL行号排序和分页(SQL查询中插入行号 自定义分页的另类实现)
-
ASP.NET MVC分页和排序功能实现
-
Python实现字典按key或者value进行排序操作示例【sorted】
-
Vue.js bootstrap前端实现分页和排序
-
java8 stream 操作map根据key或者value排序的实现
-
Vue.js实现多条件筛选、搜索、排序及分页的表格功能
-
ASP.Net MVC+Data Table实现分页+排序功能的方法
-
[ExtNet] GridPanel怎么实现服务器端分页、排序、查询?--Oracel存储过程分页
-
MVC5 + EF6 + Bootstrap3 (11) 实现排序、搜索、分页