使用 SQL 生成日期范围

2021-12-05 00:00:00 sql oracle

我有一个 SQL 查询,它接受一个日期参数(如果我要将它放入一个函数中),我需要在去年的每一天运行它.

I have a SQL query that takes a date parameter (if I were to throw it into a function) and I need to run it on every day of the last year.

如何生成过去 365 天的列表,以便我可以使用直接 SQL 来执行此操作?

How to generate a list of the last 365 days, so I can use straight-up SQL to do this?

显然生成一个列表 0..364 也可以,因为我总是可以:

Obviously generating a list 0..364 would work, too, since I could always:

SELECT SYSDATE - val FROM (...);

推荐答案

无需使用超大表或 ALL_OBJECTS 表:

There's no need to use extra large tables or ALL_OBJECTS table:

SELECT TRUNC (SYSDATE - ROWNUM) dt
  FROM DUAL CONNECT BY ROWNUM < 366

会解决问题.

相关文章