Last active
February 23, 2016 04:43
-
-
Save zmiftah/7ade92f5f0b6045e2422 to your computer and use it in GitHub Desktop.
Postgres | Per Ipp Amount Totals in Only The Top Programs
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
WITH all_ipp AS ( | |
SELECT program_id, SUM(total_amount_eqv) AS total_amount | |
FROM pdg.ipp | |
GROUP BY program_id | |
), top_ipp AS ( | |
SELECT program_id | |
FROM all_ipp | |
WHERE total_amount > (SELECT SUM(total_amount)/10 FROM all_ipp) | |
) | |
SELECT | |
program_id, | |
program_name, | |
SUM(total_amount_eqv) AS product_sales | |
FROM pdg.ipp | |
WHERE program_id IN (SELECT program_id FROM top_ipp) | |
GROUP BY program_id, program_name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment