hive 排序取中间60%数据

排序过程省略。

实现方式1:利用分位函数实现

SELECT

id

from (

select

id

,percentile(id,0.2) over () as id2

,percentile(id,0.8) over () as id8

from (

select 1 as id union all

select 2 as id union all

select 3 as id union all

select 4 as id union all

select 5 as id union all

select 6 as id union all

select 7 as id union all

select 8 as id union all

select 9 as id union all

select 10 as id union all

select 11 as id union all

select 12 as id

) as a

) as a

where id between id2 and id8

实现方式2:利用ntile桶函数 实现

NTILE(n),用于将分组数据按照顺序切分成n片,返回当前切片值。将一个有序的数据集划分为多个桶(bucket),并为每行分配一个适当的桶数(切片值,第几个切片,第几个分区等概念)。它可用于将数据划分为相等的小切片,为每一行分配该小切片的数字序号。

NTILE不支持ROWS BETWEEN,比如NTILE(2) OVER(PARTITION BY dept_no ORDER BY salary ROWS BETWEEN 3 PRECEDING - AND CURRENT ROW)。

如果切片不均匀,默认增加第一个切片的分布。

select

id

from (

select

id

,ntile(5) over(order by id asc) as bkt

from (

select 1 as id union all

select 2 as id union all

select 3 as id union all

select 4 as id union all

select 5 as id union all

select 6 as id union all

select 7 as id union all

select 8 as id union all

select 9 as id union all

select 10 as id union all

select 11 as id union all

select 12 as id

) as a

) as a

where bkt in (2,3,4)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值