Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created November 25, 2016 19:09
Show Gist options
  • Save jrjames83/677a00ae7c0863c22b2eafdf36f670f7 to your computer and use it in GitHub Desktop.
Save jrjames83/677a00ae7c0863c22b2eafdf36f670f7 to your computer and use it in GitHub Desktop.
Dvd Rental Tutorial 2 PG
SELECT p.customer_id, c.first_name,
SUM(p.amount)::money, COUNT(*),
SUM(p.amount)/COUNT(*) as AVG_RENTAL_VALUE
FROM payment p LEFT JOIN customer c ON p.customer_id = c.customer_id
WHERE p.customer_id > 300 AND c.first_name like '%b%'
GROUP BY 1,2
HAVING COUNT(*) > 20 -- having is when you filter on aggregates, is a where clause for aggregates
ORDER BY 4 DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment