How to calculate maximum age of a male person from a list containing date of birth in sql?
I tried
Select name, datediff(yy, dateofbirth, getdate()) as age
From table
Where gender = male
But I don’t know how to use the max function to get the max age
2条答案
按热度按时间bvjveswy1#
Do something like this to get the least recent date of birth.
If you use Microsoft SQL Server, you do this.
7ajki6be2#
If we assume everyone in the table is still alive, then the oldest male has the
MIN(DOB)
. You did not specify any date of death so there you go.Then you only need to calculate their age.
Something along the lines of:
For the age algorithm I used brianary's answer on a different stackoverflow thread at How to calculate age (in years) based on Date of Birth and getDate()
Feel free to upvote his answer.