我的表
Name column1 column2 column3
Julio 24 25 35
Jaime 30 40 35
这是我的查询
Select a.name, max(column1+column2+column3) from table group by name
如何获得多列总和的最大值?
答案 0 :(得分:2)
使用您的查询
select name, column1+column2+column3 as sum
from table t
join (select max(column1+column2+column3) as maxsum from table) t1
on t.column1+column2+column3 = t1.maxsum
结果将是所有名称及其列的总和,并假设每个名称将具有一个对应的行。
如果您需要与总和的最大值相对应的名称,请使用
select max(column1+column2+column3) as maxsum from table
要获得最大金额使用
Weather = LOAD 'hdfs:/home/hduser/final/Weather.csv' USING PigStorage(',');
A = FOREACH Weather GENERATE (int)$0 AS year, (int)$1 AS month, (int)$2 AS day, (int)$4 AS temp, $14 AS cond, (double)$5 as dewpoint , (double)$10 as wind;
group_by_day = GROUP A BY (year,month,day);
答案 1 :(得分:2)
我会使用order by
和fetch first 1 row only
(在Oracle 12 +中)处理此问题:
Select a.name,
sum(coalesce(column1, 0) + coalesce(column2, 0) + coalesce(column3, 0)) as s
from table
group by a.name
order by sum(coalesce(column1, 0) + coalesce(column2, 0) + coalesce(column3, 0)) desc
fetch first 1 row only;
在早期版本中,使用子查询:
select t.*
from (Select a.name,
sum(coalesce(column1, 0) + coalesce(column2, 0) + coalesce(column3, 0)) as s
from table
group by a.name
order by sum(coalesce(column1, 0) + coalesce(column2, 0) + coalesce(column3, 0)) desc
) t
where rownum = 1;
答案 2 :(得分:2)
我在SQL Server 2014中执行了此查询。 希望这会对你有所帮助
select name, max(column1+column2+column3) as MaxSum from table
group by name
having max(column1+column2+column3)= (select max(column1+column2+column3) from table)