Last active
May 23, 2017 07:52
-
-
Save drorata/2f948366524fd08ab57be44437df63d9 to your computer and use it in GitHub Desktop.
Ratio when using group by
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
| -- Compute the ratios of each type of status from the overall count | |
| -- After re-learning it over and over again, it is time to document this useful snippet. | |
| -- I hope my future-self will find it. | |
| SELECT status, | |
| c, | |
| c / cast((sum(c) over ()) AS float) AS ratio | |
| FROM | |
| (SELECT status, | |
| count(*) AS c | |
| FROM table | |
| WHERE cond1 = 1 | |
| AND date_foo < '2017-01-01' | |
| AND date_foo < '2016-09-30' | |
| GROUP BY status) AS f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment