Skip to content

Instantly share code, notes, and snippets.

@Nocks
Created November 5, 2019 04:46
Show Gist options
  • Save Nocks/1e953ab3af212a4d80e45e0f560e82a7 to your computer and use it in GitHub Desktop.
Save Nocks/1e953ab3af212a4d80e45e0f560e82a7 to your computer and use it in GitHub Desktop.
/*
Who are the top 5 active customers who have spent most in renting films?
*/
SELECT DISTINCT customer.customer_id, CONCAT(first_name, ' ', last_name) AS customer,
SUM(payment.amount) OVER(PARTITION BY customer) AS total_amt_spent
FROM customer
JOIN payment
ON customer.customer_id = payment.customer_id
WHERE customer.active = 1
ORDER BY total_amt_spent DESC
LIMIT 5;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment