--■■■decode関数を使う方法■■■
select
decode(Least(months_between(sysdate , birth_dt),168),168,
decode(greatest(months_between(sysdate , birth_dt),216),216,1,0),0)+
decode(Least(months_between(sysdate , birth_dt),217),217,
decode(greatest(months_between(sysdate , birth_dt),252),252,2,0),0)+
decode(Least(months_between(sysdate , birth_dt),253),253,
decode(greatest(months_between(sysdate , birth_dt),780),780,3,0),0)+
decode(Least(months_between(sysdate , birth_dt),781),781,4,0)+
decode(greatest(months_between(sysdate , birth_dt),167),167,null,0) as AgeGrade
from mw_clients
order by birth_dt;
--■■■スカラー副問い合わせを使う方法■■■
select
(select 1 from dual where months_between(sysdate , birth_dt) between 168 and 216 union all
select 2 from dual where months_between(sysdate , birth_dt) between 217 and 252 union all
select 3 from dual where months_between(sysdate , birth_dt) between 253 and 780 union all
select 4 from dual where months_between(sysdate , birth_dt) > 780) as AgeGrade
from mw_clients
order by birth_dt;