简体   繁体   中英

I want to find highest row from multiple columns in hive

a b c
1 1 1
2 2 2
3 3 3
4 4 4
5 5 1

here 3 columns are there and i have to find the highest row among these

result would be - 4 4 4

OK, so I've passed this round the think tank here and we find it a good puzzle trying to work out what it means. Another proposal is that 551 is the same as 155 so 444 is higher than 155.

We can turn 3,1,2 into 123 with "least number, and middle number, and greatest number". Middle number is given by the sum of all numbers minus the greatest minus the least

In this case:

SELECT * FROM table 
ORDER BY 
  LEAST(a,b,c) DESC,
  a+b+c-GREATEST(a,b,c)-LEAST(a,b,c) DESC,
  GREATEST(a,b,c) DESC 
  LIMIT 1

If your hive doesn't have greatest and least then you'll have to fake it with case when (I recommend you use a subquery)

If "highest row" means the greatest sum, then it would be

SELECT * FROM table ORDER BY a+b+c DESC LIMIT 1

if I understood you correctly? Not sure what you want to do about duplicates? What if there is a row 3 4 5?

select
    a,b,c,(a+b+c)/3
from
    table
where
    (a+b+c)/3 >= (select (avg(a)+avg(b)+avg(c))/3 from table)
and         a >= (select cast(avg(a) as int) from table)
and         b >= (select cast(avg(b) as int) from table)
and         c >= (select cast(avg(c) as int) from table)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM