gpt4 book ai didi

sql - 获取 Hive 中一行具有最大值的列

转载 作者:可可西里 更新时间:2023-11-01 14:45:18 26 4
gpt4 key购买 nike

我有一个场景,我需要从三列中选择行中的最大值,有一个名为 Greatest 的函数,但它在我的 Hive 0.13 版本中不起作用。

请建议更好的方法来完成它。

示例表:

+---------+------+------+------+
| Col1 | Col2 | Col3 | Col4 |
+---------+------+------+------+
| Group A | 1 | 2 | 3 |
+---------+------+------+------+
| Group B | 4 | 5 | 1 |
+---------+------+------+------+
| Group C | 4 | 2 | 1 |
+---------+------+------+------+

预期结果:

+---------+------------+------------+
| Col1 | output_max | max_column |
+---------+------------+------------+
| Group A | 3 | Col4 |
+---------+------------+------------+
| Group B | 5 | col3 |
+---------+------------+------------+
| Group C | 4 | col2 |
+---------+------------+------------+

最佳答案

select  col1
,tuple.col1 as output_max
,concat('Col',tuple.col2) as max_column

from (select Col1
,sort_array(array(struct(Col2,2),struct(Col3,3),struct(Col4,4)))[2] as tuple
from t
) t
;

sort_array(Array)
Sorts the input array in ascending order according to the natural ordering of the array elements and returns it (as of version 0.9.0). https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF


hive> select  col1
> ,tuple.col1 as output_max
> ,concat('Col',tuple.col2) as max_column
>
> from (select Col1
> ,sort_array(array(struct(Col2,2),struct(Col3,3),struct(Col4,4)))[2] as tuple
> from t
> ) t
> ;
OK
Group A 3 Col4
Group B 5 Col3
Group C 4 Col2

关于sql - 获取 Hive 中一行具有最大值的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42098855/

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