hive学习之六:row_number()排序函数的使用
2016-12-06 21:41
429 查看
在hive中经常需要使用到排序,hive中的排序函数有多种,可在相关文档中查阅具体的使用方法,在项目中用到了row_number()来做排序。简单的用法在这里就
不做赘述了,项目具体需求如下:
表tbl_custinfo结构如下
现在要求同一个客户下,取卡审核日期小的卡,若卡审核日期相同,取卡申请id小的卡,若卡申请id相同,则取卡种和卡申请时卡种相同的卡。
原始数据:
1002 65898 622589785645238 20161101 V9875624201 106 1027------①
1002 65898 655812318977101 20161101 V9875624201 1027 1027------②
1003 12876 688951011942014 20050521 Z1021540014 301 301-------③
一开始不知道如何实现,按卡审核日期和卡申请id排序好处理,但是要取取卡种和卡申请时卡种相同的卡就不太好处理,原先的想法是这样的:select * from
(select *,row_number() over(distribute by custno sort by recdate asc,appid asc,product=cardtype) as rank from tbl_custinfo where pt='20161015') a
where a.rank=1;mapreduce不报错,正常执行,
select * from
(select *,row_number() over(distribute by custno sort by recdate asc,appid asc,case when product=cardtype then '1' else '2' end asc) as rank from tbl_custinfo where pt='20161015') a
where a.rank=1;
结果显示是正确的。
1002 65898 622589785645238 20161101 V9875624201 1027 1027 1------①
1002 65898 655812318977101 20161101 V9875624201 106 1027 2------②
1003 12876 688951011942014 20050521 Z1021540014 301 301 1------③
不做赘述了,项目具体需求如下:
表tbl_custinfo结构如下
create table tbl_custinfo( custno string,--客户号 acctno string,--账号 cardno string,--卡号 recdate string,--卡审核日期 appid string,--卡申请id product string,--卡种 cardtype string --卡申请时卡种 ) partitioned by(pt string) row format delimited fields terminated by ',' stored as rcfile;
现在要求同一个客户下,取卡审核日期小的卡,若卡审核日期相同,取卡申请id小的卡,若卡申请id相同,则取卡种和卡申请时卡种相同的卡。
原始数据:
1002 65898 622589785645238 20161101 V9875624201 106 1027------①
1002 65898 655812318977101 20161101 V9875624201 1027 1027------②
1003 12876 688951011942014 20050521 Z1021540014 301 301-------③
一开始不知道如何实现,按卡审核日期和卡申请id排序好处理,但是要取取卡种和卡申请时卡种相同的卡就不太好处理,原先的想法是这样的:select * from
(select *,row_number() over(distribute by custno sort by recdate asc,appid asc,product=cardtype) as rank from tbl_custinfo where pt='20161015') a
where a.rank=1;mapreduce不报错,正常执行,
1002 65898 622589785645238 20161101 V9875624201 106 1027 1------① 1002 65898 655812318977101 20161101 V9875624201 1027 1027 2------② 1003 12876 688951011942014 20050521 Z1021540014 301 301 1------③但是客户号为1002的排序结果不正确,②应该排序为1。在同事的提醒下换了个思路解决:
select * from
(select *,row_number() over(distribute by custno sort by recdate asc,appid asc,case when product=cardtype then '1' else '2' end asc) as rank from tbl_custinfo where pt='20161015') a
where a.rank=1;
结果显示是正确的。
1002 65898 622589785645238 20161101 V9875624201 1027 1027 1------①
1002 65898 655812318977101 20161101 V9875624201 106 1027 2------②
1003 12876 688951011942014 20050521 Z1021540014 301 301 1------③
相关文章推荐
- hive:数据库“行专列”操作---使用collect_set/collect_list/collect_all & row_number()over(partition by 分组字段 [order by 排序字段])
- 数据库SQL中对查询结果排序排列序号编号,Oracle分析函数 rank,dense_rank,row_number使用和区别
- 关于排序rank () over , row_number() over ,rank_dense ()函数使用
- hive分组排序函数-row_number() over (partition by * order by d topN
- hive中使用自定义函数(UDF)实现分析函数row_number的功能
- hive中使用自定义函数(UDF)实现分析函数row_number的功能
- SQL Server 排序函数 ROW_NUMBER和RANK 用法总结
- 使用Oracle的分析函数ROW_NUMBER、DENSE_RANK、RANK
- SQL Server 排序函数 ROW_NUMBER和RANK 用法总结
- SQLServer2005/2008新的高效分页方法-row_number()over函数的使用
- 使用Oracle的分析函数ROW_NUMBER、DENSE_RANK、RANK
- Oracle 分析函数RANK(),ROW_NUMBER(),LAG()等的使用方法
- Oracle分析函数RANK(),ROW_NUMBER(),LAG()等的使用方法
- row_number()over函数的使用
- Oracle 分析函数RANK(),ROW_NUMBER(),LAG()等的使用方法
- 【转】Oracle分析函数RANK(),ROW_NUMBER(),LAG()等的使用方法
- SQL Server 2005四个新的排序函数: ROW_NUMBER、RANK、DENSE_RANK 和 NTILE
- SQL Server 2005四个新的排序函数: ROW_NUMBER、RANK、DENSE_RANK 和 NTILE
- ROW_NUMBER函数在分页中的使用示例
- (原创)ROW_NUMBER()函数的使用