If this is not a one-time data conversion, most likely you will be able to use the calendar table.
Having such a table makes it easy to filter or aggregate data for non-standard periods in addition to regular ISO weeks. Weeks usually behave slightly different between companies and departments within them. Once you leave ISO-land, the built-in date functions cannot help you.
create table calender( day date not null -- Truncated date ,iso_year_week number(6) not null -- ISO Year week (IYYYIW) ,retail_week number(6) not null -- Monday to Sunday (YYYYWW) ,purchase_week number(6) not null -- Sunday to Saturday (YYYYWW) ,primary key(day) );
You can create additional tables for "purchase_weeks" or "retail_weeks" or simply aggregate "on the fly":
select a.colA ,a.colB ,b.first_day ,b.last_day from your_table_with_weeks a join (select iso_year_week ,min(day) as first_day ,max(day) as last_day from calendar group by iso_year_week ) b on(a.iso_year_week = b.iso_year_week)
If you process a large number of records, on-the-fly aggregation will not make any noticeable difference, but if you execute one row, it is also useful for you to create tables for weeks.
Using calendar tables provides a slight performance advantage, as the optimizer can provide better estimates of static columns than with add_months(to_date(to_char())) nested calls.
Ronnis
source share