hive - Hive 中的列(单行)的最大值(值)

标签 hive

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

例如

Row# ID# Col1 Col2 Col3
1    1234  54  67  86
2    5678  89   92 86
...
...

寻找表单的输出:
1234 86
5678 92

谢谢!

最佳答案

从 1.1 开始,Hive 具有最大的 () 函数;

select ID, greatest(col1, col2, col3) as greatest_value from table;

或者,如果您的版本没有 best(),您可以使用 case when 语句:
select ID
, case
   when col1 >  col2 and col1 >  col3 then col1
   when col2 >  col3                  then col2
   else                                    col3
  end as greatest_value
from  table
;

Case when 语句按从上到下的顺序进行评估,直到找到 true 值,因此无需在每个 when 子句中评估两个不等式。

关于hive - Hive 中的列(单行)的最大值(值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31395324/

相关文章:

sql - 在hive窗口中,如果CURRENT ROW的值小于UNBOUNDED PRECEDING的值会发生什么

python - 如何在配置单元的 python UDF 中使用外部库?

java - 如何使用最后一个输入行转发 Hive UDTF 输出

hadoop - pig 脚本中的代理键

hadoop - 通过Amazon Elastic MapReduce上的MapReduce程序访问时无法加载Hive-JDBC驱动程序

hadoop - 在 HDP-1.3.3 上使用 kerberos 的 Oozie 配置单元操作

hadoop - Hive 查询数组作为字段

hadoop - 如何将共享 id 的多行合并为一行(HQL)?

hadoop - 跨 hadoop 实例移动部分数据

hadoop - Storm 不受控制的元组乘法