POSTGRESQL语言:
create table tmp as
select sno,avg(grade) as avg_grade from sc group by sno;
select sc.sno,sc.cno,sc.grade from sc
inner join tmp
on tmp.sno = sc.sno and sc.grade >= tmp.avg_grade;
如何将上面两句写成一句?
我这样写,不对
select sc.sno,sc.cno,sc.grade from sc a
inner join (
select b.sno,avg(b.grade) as avg_grade from sc b group by b.sno;
)
on b.sno = a.sno and a.grade >= b.avg_grade;
写成where可以实现同样的功能,但我就是想合并那两句。
select sno,cno,grade from sc as a
where grade >= (select avg(grade) from sc b where a.sno = b.sno );