gpt4 book ai didi

hadoop - 是否可以在配置单元中执行 'normalized' dense_rank()?

转载 作者:可可西里 更新时间:2023-11-01 15:27:07 26 4
gpt4 key购买 nike

我有一个这样的消费者表。

consumer | product | quantity
-------- | ------- | --------
a | x | 3
a | y | 4
a | z | 1
b | x | 3
b | y | 5
c | x | 4

我想要的是分配给每个消费者的“标准化”排名,这样我就可以轻松拆分表格以进行测试和培训。我在 hive 中使用了 dense_rank(),所以我得到了下表。

rank | consumer | product | quantity
---- | -------- | ------- | --------
1 | a | x | 3
1 | a | y | 4
1 | a | z | 1
2 | b | x | 3
2 | b | y | 5
3 | c | x | 4

这很好,但我想扩展它以用于任意数量的消费者,所以理想情况下我希望排名范围在 0 到 1 之间,就像这样。

rank | consumer | product | quantity
---- | -------- | ------- | --------
0.33 | a | x | 3
0.33 | a | y | 4
0.33 | a | z | 1
0.67 | b | x | 3
0.67 | b | y | 5
1 | c | x | 4

这样,我总是知道排名的范围是多少,并且可以以标准方式拆分数据(排名 <= 0.7 训练,排名 > 0.7 测试)

有没有办法在 hive 中实现这一点?

或者,对于我最初的拆分数据问题,是否有不同且更好的方法?

我尝试做一个 select * where rank < 0.7*max(rank) ,但配置单元表示 MAX UDAF 在 where 子句中尚不可用。

最佳答案

percent_rank

select  percent_rank() over (order by consumer) as pr
,*

from mytable
;

+-----+----------+---------+----------+
| pr | consumer | product | quantity |
+-----+----------+---------+----------+
| 0.0 | a | z | 1 |
| 0.0 | a | y | 4 |
| 0.0 | a | x | 3 |
| 0.6 | b | y | 5 |
| 0.6 | b | x | 3 |
| 1.0 | c | x | 4 |
+-----+----------+---------+----------+

要进行过滤,您需要一个子查询/CTE

select  *

from (select percent_rank() over (order by consumer) as pr
,*

from mytable
) t

where pr <= ...
;

关于hadoop - 是否可以在配置单元中执行 'normalized' dense_rank()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43129622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com