Last active
July 1, 2019 02:44
-
-
Save ByoungInKim/0ef3ff04dfcef63f988ece638fdcea55 to your computer and use it in GitHub Desktop.
Getting a percentage from MySql with a group by condition
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
SET @total=0; | |
SELECT | |
column_a, | |
column_b, | |
COUNT(*) AS cnt, | |
COUNT(*) / @total * 100 as p, | |
@total AS total | |
FROM | |
( | |
SELECT | |
column_a, | |
column_b, | |
@total := @total + 1 | |
FROM | |
table_a | |
WHERE | |
column_a is not null | |
) t | |
GROUP BY | |
column_a, | |
column_b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment