Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

benhamilton commented May 26, 2014

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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.