mysql - How to calculate age from date of birth and group each member into age range in sql -


i have sql table stores people's details i.e id, name, dob, registration_date , address. calculate age of each individual , group them these ranges: 20-30, 31-50, 51 & over.

i know can age doing: (https://stackoverflow.com/a/1572257/3045800)

select floor((cast (getdate() integer) - cast(date_of_birth integer)) / 365.25) age 

i need figure out how group people thier respective range.

thanks help

use case produce age group description:

select *,   case     when datediff(now(), date_of_birth) / 365.25 > 50 '51 & over'     when datediff(now(), date_of_birth) / 365.25 > 30 '31 - 50'     when datediff(now(), date_of_birth) / 365.25 > 19 '20 - 30'     else 'under 20'   end age_group person 

note simpler way calculate age.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -