【问题标题】:Recursive select does not return date list [closed]递归选择不返回日期列表[关闭]
【发布时间】:2021-01-23 14:50:11
【问题描述】:

我试图让一个 SELECT 从表中返回一个日期列表,从一个日期开始并将以前的日期带到它,其中包含一个开始日期和一个限制请求的数字。

SELECT sysdate - rownum 
FROM table_01 
WHERE rownum < 50;

SELECT start_date - rownum 
FROM table_01 
WHERE date_ = 'DATA_FINAL'AND rownum < date_limit;

第一个选择是我尝试使用表格列对第二个选择执行的功能示例。

Table I'm using

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。此外,样本数据和期望的结果作为文本表格会有所帮助。

标签: mysql sql date recursion


【解决方案1】:

我怀疑你想要一个“tally”表和join 为原始数据中的每一行生成多行:

with numbers as (
      select rownum as n
      from table_01
      where rownum < 50
     )
select t1.start_date - n 
from table_01 t1 join
     numbers n
     on n.n < t1.date_limit
where t1.date_ = 'DATA_FINAL';

【讨论】:

  • 你是个读心者,不可思议。
  • 效果很好,谢谢!!但是如何在减法之前显示 start_date (20200930)?
  • 您希望数字从 0 而不是 1 开始。只需在 CTE 中减去 1。
  • 非常感谢!!它工作得很好,现在也显示了 start_date!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 2013-10-15
相关资源
最近更新 更多