Create a gist now

Instantly share code, notes, and snippets.

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
Owner

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