-1

I'm trying to create a table that uses an individual's date of birth to calculate their current age then use a CASE function to group by their age range. I'm not sure if this is the correct way to pull from a temporary column or not.

SELECT CASE
            WHEN AGE < 18 then 'Under 18'
            WHEN AGE between 18 and 30 then '18-30'
            WHEN AGE > 30 then 'Over 30'
        END as age_range    
FROM Memberdata
WHERE 
    (SELECT
        Year(BIRTH) as BirthYear,
        Year(GETDATE()) as CurrentYear,
        Year(GETDATE()) - YEAR(BIRTH) as 'Age'
    FROM Memberdata)
Jalam
  • 1
  • 1
  • 2
    Have you tried execute the query ? Does it gives expected result ? – Squirrel May 18 '22 at 01:56
  • Have you heard of the datediff function? – Dale K May 18 '22 at 01:59
  • Current I'm getting 'Msg 4145, Level 15, State 1, Line 12 An expression of non-boolean type specified in a context where a condition is expected, near ')'.' The code ``` SELECT Year(BIRTH) as BirthYear, Year(GETDATE()) as CurrentYear, Year(GETDATE()) - YEAR(BIRTH) as 'Age' FROM Memberdata ``` Works by itself but I'm not too sure how to group/count them after that portion. – Jalam May 18 '22 at 01:59
  • How **precise** does your age calculation need to be? The difference is years is not very precise. – SMor May 18 '22 at 01:59

0 Answers0