with major as (
select major.dname,major.sid from (
values ('d1',101), ('d1',102),
('d1',103), ('d2',201), ('d2',202), ('d2',203)
) as major(dname,sid)
),
student as
(select sid,year from (
values (101,5),(102,5),(103,4),(104,3),(105,2),(106,1),(107,4)
) as student(sid,year)
),
temp as (
select major.dname, count(major.sid) as majors
from (
values ('d1',101), ('d1',102),
('d1',103), ('d2',201), ('d2',202), ('d2',203)
) as major(dname,sid)
group by dname
--order by majors desc
--The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
)
select dname
from temp
where majors > 2 or dname in (SELECT dname
FROM student,
major
WHERE student.sid = major.sid
AND year = 5
GROUP BY year,
dname
HAVING Count(student.sid) > 5)
輸出
dname
d1
d2
後拆除了'mysql'標籤作爲查詢清楚你的主要查詢 – 2014-10-11 06:18:28
順序不能在那裏使用(因爲CTE) – 2014-10-11 09:51:49
@anvitapotluri您應該將您在註釋中放置的主要查詢添加到您的問題中。 – 2014-10-13 13:42:29