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!
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.
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.
You can use to_date function in your where clause to get only the max(Last uploaded date) records.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With