SELECT FLOOR((EXTRACT(YEAR FROM FROM_DAYS(DATEDIFF(NOW(), birthday))) - 4) / 10) AS age, gender, COUNT(*)
FROM mytable
GROUP BY
age, gender
-1aged means 0-4, 0means 4-14, etc.
This query may leave spaces if there are no people in this age group.
The same thing with percentages (of the total population):
SELECT FLOOR((EXTRACT(YEAR FROM FROM_DAYS(DATEDIFF(NOW(), birthday))) - 4) / 10) AS age, gender,
COUNT(*) /
(
SELECT COUNT(*)
FROM mytable
)
FROM mytable
GROUP BY
age, gender
source
share