我的理解是GREATEST()和LEAST()并不是SQL标准的一部分,但是很常见。
我想知道,是否有一种方法可以在SQL标准内克隆GREATEST的功能?
SELECT id, GREATEST(1,2,3,4,5,6,7) AS number FROM table
完整查询:
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
最佳答案
您可以使用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