配置单元的聚合滑动窗口

yhqotfr8  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(430)

我有一个配置单元表,它是基于一个数值(比如count)按顺序排序的。

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

总数是27。我需要把它分成三部分。所以计数的前1/3,即1到9是1,10到18是第二,19到27是第三。我想我需要做些推拉窗的工作。

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

你知道怎么做吗

im9ewurl

im9ewurl1#

以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

相关问题