Skip to content

Instantly share code, notes, and snippets.

@brachycera
Created September 2, 2015 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brachycera/44c13f59e947ca579ae8 to your computer and use it in GitHub Desktop.
Save brachycera/44c13f59e947ca579ae8 to your computer and use it in GitHub Desktop.
Multiple count within one query
SELECT
SUM(CASE WHEN foo = '0' THEN 1 ELSE 0 END) AS bar_count,
SUM(CASE WHEN foo = '1' OR foo = '2' THEN 1 ELSE 0 END) AS baz_count
FROM
database
WHERE
baz = qux
GROUP BY baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment