I’m trying to create an age calculation column from a birthdate, and then create an age-group column based on the age column. I’ve done some searching, but all the results I can find are structured differently enough that I’m not sure I’m even approaching this in the correct way.
The query is this:
select unique ccy.bi_cust_nbr
--, ccy.bi_acct
, ccm.bi_cust_stat_cd
, p.bi_sort_name
, sl.bi_addr1
, p.bi_birth_dt
, sl.bi_board_dist_cd
--, sl.bi_srv_map_loc
, trunc(months_between(sysdate,p.bi_birth_dt)/12) as age1
, (case
when age1 between 18 and 25 then '18-24'
when age1 between 25 and 46 then '25-45'
when age1 between 46 and 66 then '46-65'
when age1 between 66 and 130 then '65+'
END) as Age_Group
from cc_ye_stat ccy
left join cc_master ccm on ccm.bi_cust_nbr=ccy.bi_cust_nbr
left join bi_personal p on p.bi_cust_nbr = ccy.bi_cust_nbr
left join bi_srv_loc sl on sl.bi_srv_loc_nbr=ccy.bi_srv_loc_nbr
where ccy.bi_srv_stat_cd in (1,3,6,7)
and p.bi_birth_dt > '01-JAN-1902'
--and ccy.bi_cust_nbr = 95051
and p.bi_birth_dt is not null
order by Age_Group desc
And the results just need to have a new column with one of the CASE results in them (18-24, etc.). I’ve got the age column working, but when I try to add the CASE section to make a new column for the age group I’m getting this error:
“ORA-00904: “AGE1”: invalid identifier
00904. 00000 – “%s: invalid identifier”
*Cause:
*Action:
Error at Line: 32 Column: 10″
which points to the last of the WHEN clauses in the CASE section.