mysql - SQL query using sum() -
hello i'm working on sql problem can't quite figure out. here schema i'm working with:
here question stuck on:
-- 3 find first name, last name , total combined film length of sci-fi films every actor. result should list names of of actors (even if actor has not been in sci-fi films) , total length of sci-fi films have been in.
so far have
select actor.first_name, actor.last_name, (select sum(film.length) film inner join film_category on film.film_id = film_category.film_id inner join category on film_category.category_id = category.category_id inner join film_actor on film_actor.film_id = film.film_id inner join actor on film_actor.actor_id = actor.actor_id category.name = 'sci-fi' ) actor
i know need group actor_id i'm unable in select subquery. have tips?
there no need use subquery. aggregate functions work on entire data set. 'group by' specifies how group data you're aggregating.
select a.actor_id, a.first_name, a.last_name, sum(f.length) actor left outer join film_actor fa on fa.actor_id = a.actor_id left outer join film f on f.film_id = fa.film_id left outer join film_categories fc on fc.film_id = f.film_id left outer join categories c on c.category_id = fc.category_id c.name = 'sci-fi' group a.actor_id ;
the outer joins ensure actors no sci-fi film experience included in results
Comments
Post a Comment