Created
November 25, 2016 19:09
-
-
Save jrjames83/677a00ae7c0863c22b2eafdf36f670f7 to your computer and use it in GitHub Desktop.
Dvd Rental Tutorial 2 PG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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