Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 困难/棘手的分析函数问题_Sql_Oracle - Fatal编程技术网

Sql 困难/棘手的分析函数问题

Sql 困难/棘手的分析函数问题,sql,oracle,Sql,Oracle,我有以下场景需要帮助 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

我有以下场景需要帮助

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\u编号预算\u期间开始\u日期结束\u日期期间\u类型 1 1 2/12/2007 2/11/2008年度 1 2 3/15/2008 3/14/2009年度 1 3 4/15/2009 4/14/2010年度 2 1 2010年8月18日2011年8月17日年度 2 2011年9月19日2012年9月18日年度 我需要的是使给定id的开始日期和结束日期连续,以便在给定id编号的预算期间内没有差距。例如,对于id编号1,最小值(开始日期)为2007年12月2日,最大值(结束日期)是2010年4月14日,因此每个预算期的数据在开始日期和结束日期之间的间隔应为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 ID\u编号预算\u期间开始\u日期结束\u日期期间\u类型 1 1 2/12/2007 2/11/2008年度 1 2 2/12/2008 2/11/2009年度 1 3 2/12/2010 4/14/2010年度 2 1 2010年8月18日2011年8月17日年度 2 2 2011年8月18日2012年9月18日年度
这可以用解析函数实现吗?我无法使用我尝试过的查询获得所需的输出。任何帮助都将不胜感激。不指定结束日期将起作用——如果下一条记录的开始日期是第二天,这是多余的。

如果每个范围是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.

发布您尝试过的查询。那么id 1呢。在您的示例请求输出中,您确实存在从2009年到2010年的差距。此外,您的2,2值是366天,再次违反了您所说的最大值364(是否应滑入预算期3?)