row_number() over (partition by 字段 a order by 计算项 b desc ) as rank 按字段 a 进行分组,按计算项 b 进行分组排序
partition by 子句指定如何分组分配 row_number,每个分组内,行的编号重新开始
order by 子句指定按哪个列排序,从而为行分配编号
如果没有 partition by ,那么整个表按 order by 的列排序
asc 默认为升序,desc 为降序
创建测试数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
create database if notexists hql;
use hql;
droptable if exists student_score;
createexternaltable if notexists student_score ( id int, class string, student_name string, score int ) row format delimited fields terminated by',' lines terminated by'\n' stored as textfile location '/hive_learn/hql/row_number';
select* from (select id, class, student_name, score, row_number() over (partitionby class orderby score desc) rank from student_score) as t where rank =1;