Hive中列(单行)的最大值(以值为单位)

use*_*244 5 hive

如何从HIVE中的一行中的不同列获取最大值?

例如

Row# ID# Col1 Col2 Col3
1    1234  54  67  86
2    5678  89   92 86
...
...
Run Code Online (Sandbox Code Playgroud)

寻找表格的输出:

1234 86
5678 92
Run Code Online (Sandbox Code Playgroud)

谢谢!

inv*_*ell 14

Hive具有1.1的最大()函数;

select ID, greatest(col1, col2, col3) as greatest_value from table;
Run Code Online (Sandbox Code Playgroud)

或者,如果您的版本没有great(),则可以使用case语句:

select ID
, case
   when col1 >  col2 and col1 >  col3 then col1
   when col2 >  col3                  then col2
   else                                    col3
  end as greatest_value
from  table
;
Run Code Online (Sandbox Code Playgroud)

如果语句按从上到下的顺序进行评估直到找到值为true,则不需要在每个when子句中评估两个不等式.