I have (from the imdb database) a casts table and a movie table. I’m trying to find the name and cast size of the movie with the largest cast (distinct actors).
This is what I came up with, but I’m getting an
“invalid column name” error on “totalcount”
Why?
Also, does this query look right?
select name, count(distinct pid) as totalcount
from casts join movie on mid=movie.id
where totalcount =
(select max(CastCount.total)
from (select count(distinct pid) as total from casts group by mid)CastCount)
group by name;
You cannot use aliases in
WHERE. Since it’s aggregate, you should replace it withHAVING:HAVING totalcount = ...Also, your subquery may return more than 1 row, and you will have another error.