views:

35

answers:

2

The compilation error says "mm" and "cc" is invalid identifier!

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;
+5  A: 

The error is because mm is the name of the Subquery Factoring (AKA CTE) instance, but as you can see:

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

You haven't declared mm as a JOIN to the m instance. Use:

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
OMG Ponies
you are brilliant, and I have no idea what is subquery factoring (aka cte) instance!
baboonWorksFine
@baboonWorksFine: The `WITH` syntax is called "Subquery Factoring" in Oracle, and "Common Table Expression" (AKA CTE) in SQL Server...
OMG Ponies
A: 

I presume you are trying to get the instructer with the most classes.

Could you not use

Select m.instructor FROM (select instructor, count(*) as c from class group by instructor order by 2 desc) m where rownum = 1
DJIDave
this will only return only 1 row, isn't it, what if there are more than one instructor teaches the most classes?
baboonWorksFine
You can try select instructor, count(\*) from class group by instructor HAVING count(\*) = (SELECT max(c) FROM (select instructor, count(\*) as c from class group by instructor) d)
DJIDave
cool. this is correct I think
baboonWorksFine