2017-07-05 6 views
-1

テーブル内に5つの列(a、b、c、d、e)が整数値であり、この表には100個の行があります。各列と列名の和はsql?つまりmax(sum(a)、sum(b)、sum(c)、sum(d)、sum(e))と列名sqlの列からの最大値の検索方法

+2

使用しているデータベースで質問にタグを付けます。 –

+1

あなたの仕事を分かち合ってください。問題を解決しようとしましたか? "https://stackoverflow.com/help/how-to-ask" –

+0

ゴードンの答えはいつもと同じです! – davejal

答えて

1

多くのデータベースでは、あなたはgreatest()を使用することができます。

あなたが caseまたはアンピボットと再凝集を使用することができ、この機能を持たないデータベースで
select greatest(sum(a), sum(b), sum(c), sum(d), sum(e)) 
from t; 

select sum(val) 
from ((select a as val, 'a' as which from t) union all 
     (select b as val, 'b' as which from t) union all 
     (select c as val, 'c' as which from t) union all 
     (select d as val, 'd' as which from t) union all 
     (select e as val, 'e' as which from t) 
    ) abcde 
group by which 
order by sum(val) desc 
fetch first 1 row only; 

これはANSI構文です。一部のデータベースは、FETCH FIRSTの代わりにTOPまたはLIMITを使用します。

関連する問題