作为"CONNECT BY级别";PostgreSQL不支持关键字,我无法将下面的sql转换为PostgreSQL
请告诉我如何在PostgreSQL上重写以下oracle SQL:
SELECT
TO_DATE('01/01/2019','MM/DD/YYYY') + level - 1 AS days,
level AS level1
FROM
dual
CONNECT BY
level <= (
SELECT
abs(TO_DATE('01/01/2021','MM/DD/YYYY') - TO_DATE('01/10/2021','MM/DD/YYYY') )
FROM
dual
WHERE
'Daily' = 'Daily'
)
结果:
01-JAN-19 1
02-JAN-19 2
03-JAN-19 3
04-JAN-19 4
05-JAN-19 5
06-JAN-19 6
07-JAN-19 7
08-JAN-19 8
09-JAN-19 9
感谢
Postgres有一个名为generate_series()
的方便函数,它使这类任务变得简单:
select dt::date, rn
from generate_series('2019-01-01'::date, '2019-01-09'::date, '1 day')
with ordinality as d(dt, rn)
我看不出Oracle查询的connect by
子句中的废弃逻辑有什么意义;标量子查询总是评估为CCD_ 3。
DB Fiddlde上的演示:
dt|rn:---------|-:2019-01-01|12019-01-02|22019-01-03|32019-01-04|42019-01-05|52019-01-06|62019-01-07|72019-01-08|82019-01-09|9