SQL 标准中的 GREATEST 和 LEAST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3794451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:40:47 来源:igfitidea点击:
GREATEST and LEAST in SQL standard
提问by WalterJ89
My understanding is that GREATEST() and LEAST() are not part of the SQL standard, but are very common.
我的理解是 GREATEST() 和 LEAST() 不是 SQL 标准的一部分,但很常见。
I'm wondering, is there a way to clone the functionality of GREATEST keeping within the SQL standard?
我想知道,有没有办法在 SQL 标准中克隆 GREATEST 的功能?
SELECT id, GREATEST(1,2,3,4,5,6,7) AS number FROM table
The fully query:
完整查询:
SELECT SUBSTR(section,1,2) AS campus,
AVG(GREATEST(maximum - enrolled, 0)) AS empty
FROM sectionrun
WHERE coursenumber = '105' AND subject = 'ENGL'
GROUP BY campus
采纳答案by Aillyn
You can use the CASE
expression:
您可以使用以下CASE
表达式:
SELECT SUBSTR(section,1,2) AS campus,
AVG(CASE WHEN maximum - enrolled > 0
THEN maximum - enrolled
ELSE 0
END) AS empty
FROM sectionrun
WHERE coursenumber = '105' AND subject = 'ENGL'
GROUP BY campus
回答by Never Sleep Again
GREATEST(1,2,3,4,5,6,7) AS number
can become
可以变成
(select max(tmp) from (
select 1 tmp from dual
union all
select 2 tmp from dual
union all
select 3 tmp from dual
union all
select 4 tmp from dual
union all
select 5 tmp from dual
union all
select 6 tmp from dual
union all
select 7 tmp from dual
) ) AS number