Forum

Posted by Iris, June 17, 2022, 5:19 a.m.

#60 movie rentals by category

Hi,

Would love some insight into why my solution is incorrect and producing the wrong avg percentage?

This is my solution:

with tablee as(
select i.film_id, c.name as category_name, count(*) as rentals
from rental r 
inner join inventory i on r.inventory_id=i.inventory_id
inner join film_category fc on i.film_id=fc.film_id
inner join category c on fc.category_id=c.category_id
where i.film_id<=10
group by 1,2)


select film_id, category_name, rentals, 
avg(rentals) over(partition by category_name) as avg_rentals_category
from tablee
group by 1,2

 

And this was the correct solution:

WITH movie_rental AS (
    SELECT
        I.film_id,
        COUNT(*) rentals
    FROM rental R
    INNER JOIN inventory I
    ON I.inventory_id = R.inventory_id
    GROUP BY I.film_id
)
SELECT 
    film_id, 
    category_name, 
    rentals, 
    avg_rentals_category 
FROM (
    SELECT
        MR.film_id,
        C.name category_name,
        rentals,
        AVG(rentals) OVER(PARTITION BY C.name) avg_rentals_category
    FROM movie_rental MR
    INNER JOIN film_category FC
      ON FC.film_id = MR.film_id
    INNER JOIN category C
      ON C.category_id = FC.category_id
) X
WHERE film_id <= 10
;

 

 

I guess I'm mainly wondering why does doing it all in one not work? And could someone help me understand the logic behind why it doesn't work. Any help appreciated thanks!

Answers

Hey Iris,

Your code looks great, (almost there), the issue was the 

where i.film_id<=10 

condition in the CTE.

With this clause, the average calculation is done on only the subset of films: (film_id <= 10 only), but our goal is to calculate the average metrics among all films, and when reporting, we are only interested in the films with id <= 10.

Here is my modification of your query which passed the MySQL OJ:

with tablee as(
  select i.film_id, c.name as category_name, count(*) as rentals
  from rental r 
  inner join inventory i on r.inventory_id=i.inventory_id
  inner join film_category fc on i.film_id=fc.film_id
  inner join category c on fc.category_id=c.category_id
  group by 1,2),

table2 as (
  select film_id, category_name, rentals, 
  avg(rentals) over(partition by category_name) as avg_rentals_category
  from table
  group by 1,2
)
select * from table2
where film_id<=10


 

Ah thank you!! This helps a lot

Iris, June 17, 2022, 1:39 p.m.
SQLPad user avatar

Leon (949)

June 17, 2022, 10:33 a.m.