Skip to content

Instantly share code, notes, and snippets.

@benhamilton
Last active August 29, 2015 14:01
Show Gist options
  • Save benhamilton/b26b032f21040ad75a0c to your computer and use it in GitHub Desktop.
Save benhamilton/b26b032f21040ad75a0c to your computer and use it in GitHub Desktop.
Example how to count how many records contain specific value, then calculate what percentage that is of the total.
SELECT
SUM(IF(field_name = 'value_one',1,0)) AS 'Value One',
SUM(IF(field_name = 'value_two',1,0)) AS 'Value Two',
ROUND(SUM(IF(field_name = 'value_one',1,0)) / (SUM(IF(field_name = 'value_one',1,0)) + SUM(IF(field_name = 'value_two',1,0))) * 100) AS 'Percentage'
FROM table_name
@benhamilton
Copy link
Author

I needed to count how many records had one value and how many records had a second value.
Then want to know what percentage the first value was of the total.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment