hive学习之六:row_number()排序函数的使用

  在hive中经常需要使用到排序,hive中的排序函数有多种,可在相关文档中查阅具体的使用方法,在项目中用到了row_number()来做排序。简单的用法在这里就

不做赘述了,项目具体需求如下:

表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------③




  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值