Sql 从两个日期列生成日期范围

Sql 从两个日期列生成日期范围,sql,oracle,plsql,oracle11g,Sql,Oracle,Plsql,Oracle11g,源表如下所示: Id start_date end_date field1 1 01/03/2019 07/03/2019 text1 2 10/04/2019 15/04/2019 text2 我希望获得以下输出: Id date field1 1 01/03/2019 text1 1 02/03/2019 text1 1 03/03/2019 text1 1 04/03/2019 text1 1 05/03/2019

源表如下所示:

Id   start_date   end_date    field1
1    01/03/2019   07/03/2019  text1
2    10/04/2019   15/04/2019  text2
我希望获得以下输出:

Id date        field1
1  01/03/2019  text1
1  02/03/2019  text1
1  03/03/2019  text1
1  04/03/2019  text1
1  05/03/2019  text1
1  06/03/2019  text1
1  07/04/2019  text1
2  10/04/2019  text2
2  11/04/2019  text2
2  12/04/2019  text2
2  13/04/2019  text2
2  14/04/2019  text2
2  15/04/2019  text2
我必须使用循环来填充此表

谢谢

您可以使用以下技巧使用“连接方式”查询


您可以使用递归CTE,这是SQL标准。例如,在以下查询中扩展的CTE在Oracle和Postgres中均适用。整个查询仅在Oracle中适用,从dual中删除并使用recursive添加后,将在Postgres中适用:


注意:由于,该查询在Oracle中无法运行,直到11.2您获得ORA-01841:全年必须介于-4713和+9999之间,并且不能为0,因为日期错误地向下移动。若您使用的是较旧的Oracle版本,那个么使用connect by可能会更好,因为使用CTE需要一些解决错误反向或预计算序列的方法,这也是很棘手的。对于较新的Oracle版本或其他供应商-如果有人发现此问题,我建议选择SQL标准方式。

输出中排除08/03、09/03、10/04等的原因是什么?此外,请向我们展示您迄今为止尝试过的查询。为什么在结果文本中,1在2019年4月7日结束,而在表中,它的结束日期为2019年3月10日?不,抱歉,这是错误的。最好使用previor而不是Distinct。我删除了我的答案,投了赞成票yours@Hadi:非常感谢。谢谢@KaushikNayak,但是如果我想在同一行中为时间间隔增加一个级别,我该怎么做呢?@KiMaN:你应该把这个问题作为链接本帖的单独问题来问。评论并不是为了提供解决方案,这回答了您最初的问题。
select id,start_date + level - 1 as "date", field1 from t 
connect by level <= end_date - start_date  + 1
    and prior id = id
and prior sys_guid() is not null;
with original as (
  select 1 as id, date '2019-03-01' as start_date, date '2019-03-07' as end_date, 'text1' as field from dual
  union
  select 2 as id, date '2019-04-10' as start_date, date '2019-04-15' as end_date, 'text2' as field from dual
), expanded (id, the_date, end_date, field) as (
  select id, start_date, end_date, field
  from original
  union all
  select original.id, expanded.the_date + 1, original.end_date, original.field
  from expanded
  join original on expanded.id = original.id and expanded.the_date + 1 <= original.end_date
)
select id, the_date, field
from expanded
order by id, the_date