-1
假設在一個表中我有5列(a,b,c,d,e)每個整數值,並且這個表有100行,我們如何才能找到最大值(a),sum(b),sum(c),sum(d),sum(e))和列名在SQL如何從列中找到最大值
假設在一個表中我有5列(a,b,c,d,e)每個整數值,並且這個表有100行,我們如何才能找到最大值(a),sum(b),sum(c),sum(d),sum(e))和列名在SQL如何從列中找到最大值
在許多數據庫中,您可以使用greatest()
:
select greatest(sum(a), sum(b), sum(c), sum(d), sum(e))
from t;
在數據庫中沒有此功能,您可以使用case
或逆轉置和reaggregate:
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語法。一些數據庫使用TOP
或LIMIT
而不是FETCH FIRST
。
用您正在使用的數據庫標記您的問題。 –
請分享你的工作,你試圖解決什麼問題?閱讀「https://stackoverflow.com/help/how-to-ask」 –
戈登的答案一如既往! – davejal