--■■■dense_rank関数を使う方法■■■
select to_char(Val,'yyyy/mm/dd') as 日,
to_char(Val,'dy') as 曜日,
dense_rank() over(partition by trunc(add_months(Val,-3),'yyyy') order by trunc(Val,'d')) as 週目
from (select to_date('20060331','yyyymmdd') + RowNum as Val
from all_catalog
where RowNum <= 365*3+1);
--■■■日曜からの日数差を求める方法1■■■
select to_char(Val,'yyyy/mm/dd') as 日,
to_char(Val,'dy') as 曜日,
trunc((Val-next_day(to_date(to_char(add_months(Val,-3),'yyyy') || '0401','yyyymmdd')-7,'日'))/7)+1 as 週目
from (select to_date('20060331','yyyymmdd') + RowNum as Val
from all_catalog
where RowNum <= 365*3+1);
--■■■日曜からの日数差を求める方法2■■■
select to_char(Val,'yyyy/mm/dd') as 日,
to_char(Val,'dy') as 曜日,
trunc((Val-trunc(to_date(to_char(add_months(Val,-3),'yyyy') || '0401','yyyymmdd'),'d'))/7)+1 as 週目
from (select to_date('20060331','yyyymmdd') + RowNum as Val
from all_catalog
where RowNum <= 365*3+1);