简体   繁体   中英

SQL error: missing right parenthesis

I want to use the query below as a sub-query in another statement, but the moment I enclose it in parenthesis, I get the error "missing right parenthesis".

Without the braces, it's executing fine.

What is the problem here? I need to enclose the statement within braces to use it as a sub-query with another statement. What do i do?

(SELECT dname 
 FROM   student, 
        major 
 WHERE  student.sid = major.sid 
        AND year = 5 
 GROUP  BY year, 
          dname 
 HAVING Count(student.sid) > 5 
 ORDER  BY year) 

You cannot use ORDER BY with a subquery inside IN . Because literally IN doesn't care about the order in which you specify the values

 with temp as (select dname, count(sid) as majors
 from major 
 group by dname
 order by majors desc)
 select dname
 from temp
 where majors > 15 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 ) 
 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 ) 

Output

dname
d1
d2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM