HiveSQL分组取topN

1 篇文章 0 订阅

 

 参考sql:
 ##统计国内,各省份的城市排名

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


 

 

 

 

 

 

 

业务sql:

select 
datacity_cn,industry_cn,guimo,tswqwt,count_date,amount 
from
(
select 
datacity_cn,industry_cn,guimo,tswqwt,count_date,amount
,row_number() over (partition by datacity_cn,industry_cn,guimo order by amount desc)  rank
from tswq_dwfx_tswqwt 
) a
where a.rank <= 10 and count_date = substr(date_sub(concat(substr(current_date,1,7),'-01'),1),1,7)

 

 

 

 

 

 

 

 

 

 

 

 

 

sql目的:
1.tswq_dwfx_tswqwt已经是结果表,amount是统计好的字段,所以没有必要再加group by字段
2.分组排序后,是取tswqwt这个字段的top10,所以分组找那个是不加tswqwt这个字段的

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值