我有以下需要帮助的方案
ID_Number Budget_Period Start_Date End_date Period_Type 1 1 2/12/2007 2/11/2008 Annual 1 2 3/15/2008 3/14/2009 Annual 1 3 4/15/2009 4/14/2010 Annual 2 1 8/18/2010 8/17/2011 Annual 2 2 9/19/2011 9/18/2012 Annual
我需要的是使给定id的开始日期和结束日期保持一致,以便在给定id_number的预算期间没有间隙。例如对于ID_Number 1,Min(Start_date)是2/12 / 2007和Max(End_date)是4/14/2010所以每个预算期间的数据应该在开始日期和结束日期之间间隔364天,如下所示
必需的输出
ID_Number Budget_Period Start_Date End_date Period_Type 1 1 2/12/2007 2/11/2008 Annual 1 2 2/12/2008 2/11/2009 Annual 1 3 2/12/2010 4/14/2010 Annual 2 1 8/18/2010 8/17/2011 Annual 2 2 8/18/2011 09/18/2012 Annual
可以使用分析函数完成吗?我无法使用我尝试的查询获得所需的输出。任何帮助将不胜感激
答案 0 :(得分:1)
如果每个范围是1年减去一天那么输出不应该是这个吗?
SQL> select id_number, i+1 budget_period, start_date, end_date, 'Annual' period_type
2 from (select id_number, min(start_date) start_date, max(end_date) end_date from a
3 group by id_number)
4 model
5 partition by (id_number)
6 dimension by (0 as i)
7 measures(start_date, end_date, end_date ed)
8 rules
9 (start_date[for i from 1 to ceil(months_between(add_months(end_date[0],-12), start_date[0])/12)
10 increment 1] = add_months(start_date[0], 12*cv(i)),
11 end_date[any] = least(ed[0], add_months(start_date[CV()], 12)-1)
12 )
13 order by 1,2;
ID_NUMBER BUDGET_PERIOD START_DAT END_DATE PERIOD
---------- ------------- --------- --------- ------
1 1 12-FEB-07 11-FEB-08 Annual
1 2 12-FEB-08 11-FEB-09 Annual
1 3 12-FEB-09 11-FEB-10 Annual
1 4 12-FEB-10 14-APR-10 Annual
2 1 18-AUG-10 17-AUG-11 Annual
2 2 18-AUG-11 17-AUG-12 Annual
2 3 18-AUG-12 18-SEP-12 Annual
7 rows selected.
答案 1 :(得分:0)
不指定结束日期是有效的 - 如果下一条记录的开始日期是后一天,那就是多余的。