Skip to content

Instantly share code, notes, and snippets.

@ByoungInKim
Last active July 1, 2019 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ByoungInKim/0ef3ff04dfcef63f988ece638fdcea55 to your computer and use it in GitHub Desktop.
Save ByoungInKim/0ef3ff04dfcef63f988ece638fdcea55 to your computer and use it in GitHub Desktop.
Getting a percentage from MySql with a group by condition
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