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