Want to create age buckets in Teradata 16.20.0.2; Database version: Teradata 16.20.53.56; Provider Version: Teradata.Net 16.20.1.0. Code throwing syntax error. Code:
select ptnt_bth_dt,
case
when ( cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date)) < 182 then '<6 mth'
when (cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date)) >= 182
and (cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date)) <365 then '6-<12 mth'
when (cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date)) >= 365
and (cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date)) < 730 then '1-<2'
when (cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365 >= 2
and round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) <= 4 then '2-4'
when round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) >= 5
and round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) <= 11 then '5-11'
when round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) >= 12
and round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) <= 17 then '12-17'
when round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) >= 18
and round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) <= 29 then '18-29'
when round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) >= 30
and round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) <= 39 then '30-39'
when round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) >= 40
and round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) <= 49 then '40-49'
when round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) >= 50
and round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) <= 64 then '50-64'
when round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) >= 65
and round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) <= 74 then '65-74'
when round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) >= 75
and round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) <= 84 then '75-84'
when round((cast(CURRENT_DATE as date) - cast(ptnt_bth_dt as date))/365) >= 85 then '85+'
else 'age missing'
end as 'Age_Groups'
from
select
DATE '2022-12-30' AS ptnt_bth_dt
Error: "SELECT Failed. [3706] Syntax error: expected something between the word 'case ' and the 'when' keyword."
Tried different ways of converting to date or using elseif statements
Update: Modified the query to:
select t1.*,
case when t1.age_in_years between 2 and 4 then '2-4'
when t1.age_in_years between 5 and 11 then '5-11'
when t1.age_in_years between 12 and 17 then '12-17'
when t1.age_in_years between 18 and 29 then '18-29'
when t1.age_in_years between 30 and 39 then '30-39'
when t1.age_in_years between 40 and 49 then '40-49'
when t1.age_in_years between 50 and 64 then '50-64'
when t1.age_in_years between 65 and 74 then '65-74'
when t1.age_in_years between 75 and 84 then '75-84'
when t1.age_in_years >= 85 then '85+'
else 'NA'
end as Age_Groups
from
(...)t1
Error:
SELECT Failed. [3707] Syntax error, expected something like an 'END' keyword between a string or a Unicode character literal and the word ' '.
Also (...)t1 inner query runs successfully