Hive 中的列(单行)的最大值(值) [英] Max (in value) of columns (in a single row) in Hive

查看:30
本文介绍了Hive 中的列(单行)的最大值(值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get max value from different columns from a row in HIVE?

例如

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

寻找表单的输出:

1234 86
5678 92

谢谢!

推荐答案

Hive 在 1.1 中拥有最伟大的() 函数;

Hive has the greatest() function as of 1.1;

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

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

Or you can use a case when statement if your version doesn't have greatest():

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 子句中评估两个不等式.

Case when statements are evaluated in order from top to bottom until a value of true is found, so no need to evaluate two inequalities in each when clause.

这篇关于Hive 中的列(单行)的最大值(值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆