hive分组排序 取top N

转自:http://blog.csdn.net/longshenlmj/article/details/50525385

hive中比较麻烦,没有直接实现的函数,可以写udf实现。还有个比较简单的实现方法:
用row_number,生成排名序列号。然后外部分组后按这个序列号多虑,样例代码如下
select a.*
from(   
    select 品牌,渠道,档期,count/sum/其它() as num row_number() over (partition by 品牌,渠道 order by num desc ) rank
    from table_name
    where 品牌,渠道 限制条件
    group by 品牌,渠道,档期
    )a
where a.rank<=10
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

其实 排序有三个函数 
(1)row_number:排序后,顺序下来,相同项按先后顺序排序,1,2,3,4,5 
(2)rank:排序后,遇到数据相同项时序号一致,后面并留空一位,比如,1,2,2,4,4,6

dense_rank:在遇到数据相同项时,序号一致,不留空位,如 1,2,2,3,3,4,4,5

具体用例可以参见:http://www.cnblogs.com/dycg/p/4260283.html

我自己设计的代码

##统计国内,各省份的城市排名
select b.*
from
(select country,
    province,
    city,
    cnt,
    row_number() over (partition by country,province order by cnt desc) rank
from 
    (select country,
            province,
            city,
            count(1) as cnt
    from tb_pmp_region_report_hive_mapping
    where country = '中国'
    group by country,province,city
    ) a
)b
where b.rank<=3
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

表a统计出基本数据,从a中加排名项。然后,按排名项过滤。内部group后,外部不需要group by

需要注意的是,加排名项时,不应该使用group。
如果有group,那么row_number中的order by项必须是group内的字段,否则报错,如下段代码报错
select b.*
from
(select country,
    province,
    city,
    cnt,
    row_number() over (partition by country,province order by cnt desc) rank
from 
    (select country,
            province,
            city,
            count(1) as cnt
    from tb_pmp_region_report_hive_mapping
    where country = '中国'
    group by country,province,city
    ) a
    group by country,province,city
)b
where b.rank<=3
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

执行报错: 
FAILED: SemanticException Failed to breakup Windowing invocations into Groups. At least 1 group must only depend on input columns. Also check for circular dependencies. 
Underlying error: org.apache.hadoop.hive.ql.parse.SemanticException: Line 7:62 Expression not in GROUP BY key ‘cnt’ 
hive>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值