Skip to content

Instantly share code, notes, and snippets.

@drorata
Last active May 23, 2017 07:52
Show Gist options
  • Select an option

  • Save drorata/2f948366524fd08ab57be44437df63d9 to your computer and use it in GitHub Desktop.

Select an option

Save drorata/2f948366524fd08ab57be44437df63d9 to your computer and use it in GitHub Desktop.
Ratio when using group by
-- 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