hive:函数:排名函数:Rank(笔记)
Rank
1.函数说明
RANK() 排序(排名)相同时会重复,总数不会变DENSE_RANK() 排序(排名)相同时会重复,总数会减少ROW_NUMBER() 依次进行排名
2.数据准备
表6-7 数据准备
name |
subject |
score |
孙悟空 |
语文 |
87 |
孙悟空 |
数学 |
95 |
孙悟空 |
英语 |
68 |
大海 |
语文 |
94 |
大海 |
数学 |
56 |
大海 |
英语 |
84 |
宋宋 |
语文 |
64 |
宋宋 |
数学 |
86 |
宋宋 |
英语 |
84 |
婷婷 |
语文 |
65 |
婷婷 |
数学 |
85 |
婷婷 |
英语 |
78 |
3.需求
计算每门学科成绩排名
4.创建本地score.txt,导入数据
vi score.txt
5.创建hive表并导入数据
create table score(
name string,
subject string,
score int)
row format delimited fields terminated by "\t";load data local inpath '/opt/module/datas/score.txt' into table score;
6.按需求查询数据
select name,subject,score,rank() over(partition by subject order by score desc) rank,dense_rank() over(partition by subject order by score desc) dense_rank,row_number() over(partition by subject order by score desc) row_number
from tmp.score;
扩展:求出每门学科前三名的学生?
select * from
(selectname,subject,score,rank() over(partition by subject order by score desc ) rank,dense_rank() over(partition by subject order by score desc) dense_rank,row_number() over(partition by subject order by score desc) row_numberfrom tmp.score
) s where row_number<=3
注意:partition后边有哪个字段只会影响最后的rank的排名,跟去重没关系,select后边字段可以指定多个,不一定跟partition后边的字段一致。
举例:
=================sql1:select create_time,creator_id,fullname,expectCity,positionName,keywords,row_number() over(partition by creator_id,fullname order by create_time desc) as rankfrom ( select from_unixtime(ceil(c.create_time/1000),'yyyy-MM-dd') create_time,c.creator_id,e.fullname,c.expectCity,c.positionName,c.keywords,c.degree,c.minage,c.maxage,c.isNationalUnified,c.isFamous,c.companyName,c.expectIndustry,c.workCity,c.gender,c.minCurrentSalary,c.maxCurrentSalary,c.minWorkYear,c.maxWorkYear--,concat(expectCity,':',positionName,'(',keywords,' ',degree,' ',minage,'-',maxage,')') conditions from tmp.t_conditions c--select c.creator_id,explode(split(regexp_replace(regexp_replace(expectCity,'\\[',''),'\\]',''),',')) city from tmp.t_conditions cleft join ods.ods_aimsen_walre_anlle_base_employees eon c.creator_id=e.numberwhere c.creator_id not in ('130000062','130000063','130000071','430000003')and c.tag in(2)and c.phone = ''and c.resumeId = ''and c.name = ''and from_unixtime(ceil(c.create_time/1000),'yyyy-MM-dd') > '2020-07-20'--and expectCity !='' and degree !='' and (maxAge!='' or minAge!='') and (positionName !='' or keyWords!='')and creator_id=100400287group by create_time,c.creator_id,e.fullname,c.expectCity,c.positionName,c.keywords,c.degree,c.minage,c.maxage,c.isNationalUnified,c.isFamous,c.companyName,c.expectIndustry,c.workCity,c.gender,c.minCurrentSalary,c.maxCurrentSalary,c.minWorkYear,c.maxWorkYear--,concat(expectCity,':',positionName,'(',keywords,' ',degree,' ',minage,'-',maxage,')')) x=================sql2:select create_time,creator_id,fullname,expectCity,positionName,keywords,row_number() over(partition by creator_id,fullname,expectCity order by create_time desc) as rankfrom ( select from_unixtime(ceil(c.create_time/1000),'yyyy-MM-dd') create_time,c.creator_id,e.fullname,c.expectCity,c.positionName,c.keywords,c.degree,c.minage,c.maxage,c.isNationalUnified,c.isFamous,c.companyName,c.expectIndustry,c.workCity,c.gender,c.minCurrentSalary,c.maxCurrentSalary,c.minWorkYear,c.maxWorkYear--,concat(expectCity,':',positionName,'(',keywords,' ',degree,' ',minage,'-',maxage,')') conditions from tmp.t_conditions c--select c.creator_id,explode(split(regexp_replace(regexp_replace(expectCity,'\\[',''),'\\]',''),',')) city from tmp.t_conditions cleft join ods.ods_aimsen_walre_anlle_base_employees eon c.creator_id=e.numberwhere c.creator_id not in ('130000062','130000063','130000071','430000003')and c.tag in(2)and c.phone = ''and c.resumeId = ''and c.name = ''and from_unixtime(ceil(c.create_time/1000),'yyyy-MM-dd') > '2020-07-20'--and expectCity !='' and degree !='' and (maxAge!='' or minAge!='') and (positionName !='' or keyWords!='')and creator_id=100400287group by create_time,c.creator_id,e.fullname,c.expectCity,c.positionName,c.keywords,c.degree,c.minage,c.maxage,c.isNationalUnified,c.isFamous,c.companyName,c.expectIndustry,c.workCity,c.gender,c.minCurrentSalary,c.maxCurrentSalary,c.minWorkYear,c.maxWorkYear--,concat(expectCity,':',positionName,'(',keywords,' ',degree,' ',minage,'-',maxage,')')) x
sql1和sql2区别为:
sql2多个expectCity字段
create_time,creator_id,fullname,expectCity,positionName,keywords,row_number() over(partition by creator_id,fullname,expectCity order by create_time desc) as rank
sql1查询效果:
sql2查询效果:
rank排名发生了变化,但最终查询出的结果条数并没有改变。
hive:函数:排名函数:Rank(笔记)相关推荐
- Hive开窗函数——排名函数开窗
排名函数开窗 rank() over 作用:查出指定条件后的进行排名,条件相同排名相同,排名间断不连续. 说明:例如学生排名,使用这个函数,成绩相同的两名是并列,下一位同学空出所占的名次.即:1 1 ...
- SQL新函数, 排名函数 - ROW_NUMBER(), RANK(), DENSE_RANK()
ROW_NUMBER() 根据over后的order by字据的字段排序,返回一个不断递增的整数. use Northwind go select CompanyName, ContactName ...
- SQL排序函数+排名函数使用
1要求取第二高的薪水 解题思路:可以结合order by 和limit函数 详细介绍(很多年前从一个博主大大那边截图下来的笔记,觉得非常详细) 2要求展示分数排名,分数相同排名相同,后续排名连续不跳跃 ...
- 基于hive的SQL排名函数详解
前言 在hive的学习中接触到了三种很实用的排名函数,这些函数在Mysql8.0版本中也已存在, 分别是:Rank(),Dense_rank(),Row_number(), 虽然都是排名函数,但三者间 ...
- Oracle笔记 之 分组统计排名函数dense_rank/rank()-over()函数
分组统计排名函数 解析 dense_rank/rank()-over()用于对数据进行统计排名. rank-over()函数对相同的统计值的排名相同,不同的统计值之间的排名不连续(相同统计值的排名均为 ...
- hive窗口函数之排名函数row_number、rank和dense_rank
文章目录 简单排名 分组排名 练习1-取前两名学生 练习2-连续签到次数 排名函数主要有row_number.rank和dense_rank. 简单排名 row_number是行号,不会重复,rank ...
- R语言dplyr包使用arrange函数、group_by函数、mutate函数生成分组数据的排名(rank)实战(Rank Variable by Group):升序排名、降序排名以及相同排名的处理
R语言dplyr包使用arrange函数.group_by函数.mutate函数生成分组数据的排名(rank)实战(Rank Variables by Group):升序排名.降序排名以及相同排名的处 ...
- excel排名_WPS-Excel表格实用技巧——排位排名函数(RANK函数)的使用
平时使用excel表格处理数据时经常会用到排序功能,一般使用"升序"和"降序"工具按钮就可以完成简单的排序任务,但是当我们遇到相同数值在总体中的排名时,简单的& ...
- SQL2005 四个排名函数(row_number、rank、dense_rank和ntile)的比较
排名函数是SQL Server2005新加的功能.在SQL Server2005中有如下四个排名函数row_number.rank.dense_rank和ntile,需要的朋友可以参考下. 排名函数是 ...
- SQL With As 用法Sql 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
Sql 四大排名函数(ROW_NUMBER.RANK.DENSE_RANK.NTILE)简介 排名函数是Sql Server2005新增的功能,下面简单介绍一下他们各自的用法和区别.我们新建一张Ord ...
最新文章
- JS函数的定义与调用方法
- decisiontreeregressor_机器学习算法-Decision Tree
- 图 深度优先遍历 广度优先遍历 非递归遍历 图解算法过程
- characteriscation in English debate
- LambdaNetworks解读
- 控制台应用和空项目有什么区别_互联网小程序的应用以及APP的应用有什么区别及发展...
- Flask之Web表单使用
- 快速学习javascript 整体架构方法
- 如何创建C语言的项目
- Python爬虫实战|爬取视频
- 排列组合的写法_排列组合公式探究
- 双11到了,月薪5千的人如何赚到2万?这里有5个技巧!
- NumberFormat以及其子类的使用
- 卖货文案不会写?试试这5个方法(二)
- 52-20210322华为海思Hi3516DV300的linux系统编译(eMMC模式)1
- ~7 ccf 2021-04-1 灰度直方图
- 网易云课堂 Service Worker 运用与实践
- 杂项-操作系统:操作系统百科
- 中科图新地图软件(LocaSpaceViewer)的各种卫片的偏移对比
- 【免费压缩图片的在线地址】