Instructions
Write a query to return the number of productive and less-productive actors. The order of your results doesn't matter.
Definitions
productive: appeared in >= 30 films. less-productive: appeared in <30 films.
I got this error on my query below
(syntax error at or near "AS" LINE 14: END) AS actor_category ^)
SELECT a.actor_id, MAX(a.first_name), MAX(a.last_name)
FROM actor a
INNER JOIN film_actor fa
ON a.actor_id = fa.actor_id
INNER JOIN film f
ON fa.film_id = f.film_id
(CASE
WHEN a.actor_id >= 30 THEN 'productive'
WHEN a.actor_id <= 30 THEN 'less-productive'
END) AS actor_category
GROUP BY a.actor_id;
This was the answer I was given:
SELECT actor_category,
COUNT(*)
FROM (
SELECT
A.actor_id,
CASE WHEN COUNT(DISTINCT FA.film_id) >= 30 THEN 'productive' ELSE 'less productive' END AS actor_category
FROM actor A
LEFT JOIN film_actor FA
ON FA.actor_id = A.actor_id
GROUP BY A.actor_id
) X
GROUP BY actor_category;
Why does it have to be done this way?