Skip to content

Instantly share code, notes, and snippets.

@Nocks
Last active November 6, 2019 00:07
Show Gist options
  • Save Nocks/c7d22e52f622b24cef234e1b8221944a to your computer and use it in GitHub Desktop.
Save Nocks/c7d22e52f622b24cef234e1b8221944a to your computer and use it in GitHub Desktop.
/*
What are the top 10 film categories where most money have been received?
*/
SELECT DISTINCT category.category_id,
category.name category,
COUNT(category) OVER(PARTITION BY category) AS rental_count,
ROUND(AVG(payment.amount) OVER(PARTITION BY category), 2) AS avg_amt_received,
SUM(payment.amount) OVER(PARTITION BY category) AS total_amt_received
FROM film
JOIN film_category
ON film_category.film_id = film.film_id
JOIN category
ON film_category.category_id = category.category_id
JOIN inventory
ON film.film_id = inventory.film_id
JOIN rental
ON inventory.inventory_id = rental.inventory_id
JOIN payment
ON rental.rental_id = payment.rental_id
ORDER BY total_amt_received DESC
LIMIT 10;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment