Last active
August 29, 2015 14:01
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.