I have table user that has user_id
, user_name
and user_dob
.
I want to count how many users that are under 18 year old, 18-50 and over 50.
The Age calculation method need to be improved to calculate exact age but for now I am more interested in finding the method to count
So I tried:
SELECT COUNT ([user_id])
from [user]
where (DATEDIFF(yy,[user_dob], GETDATE()) < 18)
UNION ALL
SELECT COUNT ([user_id])
from [user]
where (DATEDIFF(yy,[user_dob], GETDATE()) >= 18 AND DATEDIFF(yy,[user_dob], GETDATE()) <=50)
UNION ALL
SELECT COUNT ([user_id])
from [user]
where (DATEDIFF(yy,[user_dob], GETDATE()) > 50)
It gives me result like:
(No column name)
1218
3441
1540
But I need something like this
Range | Count
----------------
Under 18 | 1218
18-50 | 3441
Over 50 | 1540
Any suggestions how to archive above format?