0

我有以下情况需要帮助

ID_Number Budget_Period Start_Date End_date Period_Type
   1 1 2007 年 2 月 12 日 2008 年 2 月 11 日 年度
   1 2 2008 年 3 月 15 日 2009 年 3 月 14 日 年度
   1 3 2009 年 4 月 15 日 2010 年 4 月 14 日 年度
   2 1 2010 年 8 月 18 日 2011 年 8 月 17 日 年度
   2 2 2011 年 9 月 19 日 2012 年 9 月 18 日 年度

我需要的是使给定 id 的开始日期和结束日期连续,以便给定 id_number ..so 的预算期间之间没有间隙,例如 ID_Number 1 , Min(Start_date) 是 2/12/2007 和Max(End_date) 是 2010 年 4 月 14 日,因此每个预算期的数据在开始日期和结束日期之间应仅相隔 364 天,如下所示

所需输出

ID_Number Budget_Period Start_Date End_date Period_Type
   1 1 2007 年 2 月 12 日 2008 年 2 月 11 日 年度
   1 2 2008 年 2 月 12 日 2009 年 2 月 11 日 年度
   1 3 2010 年 2 月 12 日 2010 年 4 月 14 日 年度
   2 1 2010 年 8 月 18 日 2011 年 8 月 17 日 年度
   2 2 2011 年 8 月 18 日 2012 年 9 月 18 日 年度

这可以使用分析函数来完成吗?我无法使用我尝试过的查询获得所需的输出。任何帮助将不胜感激

4

2 回答 2

1

if each range is to be 1 year minus a day then shouldn't the output be this?

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.
于 2012-12-01T01:38:16.160 回答
0

不指定结束日期是可行的——如果下一条记录的开始日期是后天,那就多余了。

于 2012-12-01T00:01:54.480 回答