sql—获取配置单元中具有行的最大值的列

clj7thdc  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(331)

我有一个场景,我需要从三列中选取行中的最大值,有一个名为magest的函数,但它在我的hive0.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       |
+---------+------------+------------+
j5fpnvbx

j5fpnvbx1#

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
;

排序数组(array)
根据数组元素的自然顺序将输入数组按升序排序并返回它(从版本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

相关问题