【问题标题】:Aggregate Sliding Window for hive蜂巢的聚合滑动窗口
【发布时间】:2020-08-28 02:55:23
【问题描述】:

我有一个 hive 表,它是根据数值排序的,比如 count。

fruit   count
------  -------
apple   10
orange  8
banana  5
melon   3
pears   1

总数为 27。我需要将它分成三个部分。所以计数的前 1/3 即 1 到 9 是一,10 到 18 是第二个,19 到 27 是第三个。 我想我需要做一些滑动窗口。

fruit   count    zone
------  ------- --------
apple   10      one
orange  8       two
banana  5       three
melon   3       three
pears   1       three

知道如何解决这个问题

【问题讨论】:

  • 如果第一个计数是 50,结果会是什么? (即超过总值的2/3s。)

标签: sql hive hiveql sliding-window


【解决方案1】:

SQL方式:

select *,
(
sum(count)  over (partition by 1 order by count desc) /*<---this line for return running totals*/
/(sum(count) over (partition by 1) /3) /*<-- divided total count into 3 group. In your case this is 9 for each zone value.*/
) /*<--using running totals divided by zone value*/
+ /*<-- 11 / 9 = 1 ... 2  You must plus 1 with quotient to let 11 in the right zone.Thus,I use this + operator  */
(
case when 
(
sum(count)  over (partition by 1 order by count desc)
%(sum(count) over (partition by 1) /3) /*<--calculate remainder */
) >1 then 1 else 0 end /*<--if remainder>1 then the zone must +1*/
)  as zone
from yourtable

【讨论】:

  • + 运算符代表什么?
  • @JohnConstantine 18 和 23 除以 9 是 2。23 属于区域 3。因为在您的示例中,10 属于区域 1,所以当余数 >1 时,我使用 + 运算符加 1。如果你想要 10 个属于区域 2,则当余数 >0 时加 1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 2019-09-29
  • 2021-08-04
  • 2023-03-23
相关资源
最近更新 更多