Skip to content

Instantly share code, notes, and snippets.

@ahafidi
Last active March 22, 2025 09:57
Show Gist options
  • Save ahafidi/2b3d7d52f39eeb91d355330102f07e14 to your computer and use it in GitHub Desktop.
Save ahafidi/2b3d7d52f39eeb91d355330102f07e14 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE TEMP TABLE AVERAGE_RATINGS_BY_YEAR_GENRE AS
WITH T1 AS ( -- T1 -> Common Table Expression (CTE)
SELECT r.rating, r.year, unnest(f.genres) AS genre
FROM Films f JOIN Ratings r ON (r.film_id = f.film_id)
)
SELECT year, genre, avg(rating) AS average_rating
FROM T1
GROUP BY year, genre
ORDER BY year DESC, genre
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment