Voting data is collected to this table |
So here is what my query looks like.
SELECT ages.age_s +Sep.s+ages.age_e as AgeGroup ,COUNT(T)as Count
FROM (SELECT ' - ' AS s) AS Sep
CROSS JOIN
(SELECT '18' AS age_s, '24' AS age_e
UNION ALL
SELECT '25' AS age_s, '34' AS age_e
UNION ALL
SELECT '35' AS age_s, '44' AS age_e
UNION ALL
SELECT '45' AS age_s, '54' AS age_e
UNION ALL
SELECT '55' AS age_s, '64' AS age_e
UNION ALL
SELECT '65' AS age_s, '74' AS age_e
UNION ALL
SELECT '75' AS age_s, '100' AS age_e) AS ages
LEFT OUTER JOIN
(SELECT ((DATEPART(year, CONVERT (date, GETDATE())
))-(DATEPART(year, dob))) as T from VotingInfo ) AS m
ON T BETWEEN ages.age_s AND age_e
GROUP by ages.age_s +Sep.s+ages.age_e
ORDER BY ages.age_s +Sep.s+ages.age_e asc
Output of the above query |
Explanation:
- 'age_s' and 'age_e' is boundary of age groups like 18-24,25-34
- 'dob' is the field name where i stored Dob of those who have voted
- 'VotingInfo' is the table name from where i collect date
Data visualized in the application |