Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max (in value) of columns (in a single row) in Hive

Tags:

hive

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

For instance

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

Looking for output of the form:

1234 86
5678 92

Thanks!

like image 835
user1661244 Avatar asked Jul 13 '15 23:07

user1661244


People also ask

How do I find the maximum of a column in hive?

Firstly, enter the database using the use demo; command and list all the tables in it using the show tables; command. Let us also look at the user_info table schema using the describe user_info; command. MAX(): The MAX function is used to compute the maximum rows in the column or expression.

What is limit in Hive query?

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants. The first argument specifies the offset of the first row to return (as of Hive 2.0.

How do I SELECT Max date in hive?

You can use to_date function in your where clause to get only the max(Last uploaded date) records.


1 Answers

Hive has the greatest() function as of 1.1;

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

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 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.

like image 132
invoketheshell Avatar answered Oct 15 '22 06:10

invoketheshell