分析窗口函数 order by 时,desc NULL 值排在末尾,ASC时NULL值排在首位, hive中没有类似oracle row_number () over(partition by 字段 order by 字段 desc NULLS last) 这种写法; 所以使用前需要对空值做处理,否则影响最终的结果
测试表数据:
create table px_data_test_temp.test_stu(
id int comment '学生id',
name string comment '学生姓名'
) comment '学生信息表';
insert into table px_data_test_temp.test_stu
(id,name) values
(1, "xiaoming"),
(2, "xiaohei"),
(3, "xiaohong"),
(4, "xiaobai"),
(5, "xiaolv"),
(null, "aaaaa");
测试1:
select
id, name, row_number() over ( order by id asc ) as rk
from test_stu;
结果:
测试2:
select
id, name, row_number() over ( order by id desc ) as rk
from test_stu;
结果:
解决办法:新增一个字段 case when 字段 is null then 1 else 0 end as paixu,然后排序的时候,先按照这个字段排序;
测试3:
select
id, name, paixu, row_number() over ( order by paixu desc, id asc) as rk
from
(
select if(id is null, 0, 1) as paixu, id, name from test_stu
) temp;
结果: