简体   繁体   中英

SQL Expression missing error

I am trying to select from a table with the average of columns from other tables from other tables; The schema is as follows.

Students ( sid , firstname , l astname , status , gpa , email )

Enrollments ( sid , classid , lgrade )

Grades ( lgrade , ngrade )

And the erroneous query is,

select sid, lastname, 
avg( select g.ngrade from grades g, enrollments e 
    where e.sid = sid and g.lgrade = e.lgrade and e.lgrade is not null 
    and g.ngrade is not null) as cgpa
from students
order by cgpa asc;

从子查询中取出分号。

There are several issues:

  • Remove the semicolon from the subquery.
  • You're attempting to use a correlated subquery as a value expression. That's fine, but you have to wrap it in parentheses to do so. I only see one set of parentheses, the ones for the avg() function.
  • You're also missing an alias on a column. For clarity in expressing your intent, you should be consistent and use aliases throughout.
  • Last, a GROUP BY clause is required somewhere.

All in all, I think the aggregate should be inside the parentheses.

Try:

select
   sid,
   lastname, 
   (
      select avg(g.ngrade)
      from grades g, enrollments e 
      where e.sid = sid and g.lgrade = e.lgrade
      and g.ngrade is not null
   ) as cgpa
from students
order by cgpa asc;

Other notes: e.lgrade is not null is not needed since the condition g.lgrade = e.lgrade already ensures it won't be null.

Last, I encourage you to learn ANSI join syntax. You'll thank me later. Seriously, using old-style joins is awful.

select
   s.sid,
   s.lastname, 
   (
      select avg(g.ngrade)
      from
         grades g
         inner join enrollments e
            on g.lgrade = e.lgrade
      where
         g.ngrade is not null
         and s.sid = g.sid
   ) as cgpa
from students s
order by cgpa asc;

In fact, I have a suspicion that simply rewriting the query this way will help expose what's wrong with it--it looks to me like maybe the grades and enrollments tables need another join condition?

Two INNER JOIN s without sub-queries - please try.

SELECT s.sid, s.lastname, avg(g.ngrade) cgpa
FROM Students s
JOIN Enrollments e ON s.sid = e.sid
JOIN Grades g ON e.lgrade=g.lgrade
GROUP BY s.sid, s.lastname
ORDER BY 3;

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