oracle 10g sql带子句编译错误

时间:2010-10-25 19:53:58

标签: sql oracle oracle10g ora-00904

编译错误说“mm”和“cc”是无效的标识符!

with m as (
  select instructor, 
         count(*) as c 
    from class 
group by instructor),
     mm as ( 
  select max(m.c) as cc 
    from m)
select m.instructor 
  from m 
 where m.c = mm.cc;

2 个答案:

答案 0 :(得分:5)

错误是因为mm是子查询因子(AKA CTE)实例的名称,但正如您所见:

SELECT m.instructor 
 FROM m 
WHERE m.c = mm.cc;

您尚未将mm声明为m实例的JOIN。使用:

WITH m AS (
    SELECT instructor, 
           COUNT(*) as c 
      FROM CLASS
  GROUP BY instructor),
     mm AS ( 
    SELECT MAX(m.c) as cc 
      FROM m)
SELECT m.instructor 
  FROM m
  JOIN mm ON mm.cc = m.c

答案 1 :(得分:0)

我认为你正在努力让教师掌握最多的课程。

你能否使用

Select m.instructor FROM (select instructor, count(*) as c from class group by instructor order by 2 desc) m where rownum = 1