我正在使用带有附加表scott
的默认oracle PROJECT
数据库,该数据库有5列:projno,projname,budget,start_date,end_date。
我想选择特定年份中项目数量最多的月份。 在我的练习指导中,据说必须通过相关子查询来完成。
我知道如何使用uncorrelated subquery
:
SELECT EXTRACT(month FROM end_date) as "Month", COUNT(*) as "No of projects"
FROM proj
WHERE EXTRACT(year FROM end_date) = 2016
GROUP BY EXTRACT(month FROM end_date)
HAVING COUNT(*) = (SELECT MAX(COUNT(*))
FROM proj
WHERE EXTRACT(year FROM end_date) = 2016
GROUP BY EXTRACT(month FROM end_date))
这是我对correlated subquery
的尝试-它不起作用:
SELECT (EXTRACT(month FROM end_date)), COUNT(*) as "liczba"
FROM proj p
WHERE EXTRACT(year FROM end_date) = 2016
GROUP BY EXTRACT(month FROM end_date)
HAVING COUNT(*) = MAX (SELECT(COUNT(EXTRACT(month FROM proj.end_date)))
FROM proj
WHERE EXTRACT(month FROM proj.end_date) = EXTRACT(month FROM p.end_date)
AND EXTRACT(year FROM proj.end_date) = 2016)