PostgreSQL 时间维度(按小时和天)错误

PostgreSQL Time Dimension (By Hours and Days) Error

我正在使用 DATE_ID 和 DATE_DESC 在 PostgreSQL 中构建时间维度 table。 我的 T-SQL(完美运行)脚本是:

set DATEFIRST 1
 ;WITH DATES AS (
    SELECT CAST('2019-01-01 00:00:00.000' AS datetime) AS [DATE]
    UNION ALL
    SELECT DATEADD(HH,1,[DATE])
    FROM DATES
    WHERE DATEADD(HH,1,[DATE]) <= CAST('2019-12-31' AS datetime)
)
SELECT
DATE_ID, DATE_DESC
from
(
SELECT  
CONVERT(int, CONVERT(char(8), DATE, 112)) AS DATE_ID,
DATE AS DATE_DESC
FROM
    DATES)a
order by 1
OPTION (MAXRECURSION 0)

目前我正在尝试将此代码转换为 PostgreSQL 可读代码,但它不起作用.. 这是我现在的:

set EXTRACT(DOW FROM TIMESTAMP  '2019-01-01 00:00:00.000')+1
;WITH DATES AS (
    SELECT CAST('2019-01-01 00:00:00.000' AS timestamp) AS DATE
    UNION ALL
    SELECT CURRENT_DATE + INTERVAL '1 hour'
    FROM DATES
    WHERE CURRENT_DATE + INTERVAL '1 hour' <= CAST('2019-12-31' AS timestamp)
)

SELECT DATE_ID, DATE_DESC from
(SELECT  cast(to_char((DATE)::TIMESTAMP,'yyyymmddhhmiss') as BIGInt) AS DATE_ID,
DATE AS DATE_DESC
FROM
    DATES)a
order by 1
OPTION (MAXRECURSION 0)

我需要 2019-01-01 和 2019-12-31 之间的所有时间(24 小时)。目前我认为 OPTION (MAXRECURSION 0)set EXTRACT(DOW FROM TIMESTAMP '2019-01-01 00:00:00.000')+1 没有正常工作。

这是递归 CTE 的问题,在 Postgresql 中,您想要的查询如下所示

WITH recursive DATES AS (
    SELECT CAST('2019-01-01 00:00:00.000' AS timestamp) AS date_
    UNION ALL
    SELECT date_ + INTERVAL '1 hour'
    FROM DATES
    WHERE date_ + INTERVAL '1 hour' <= CAST('2019-12-31' AS timestamp)
)

SELECT DATE_ID, DATE_DESC from
(SELECT  cast(to_char((date_)::TIMESTAMP,'yyyymmddhhmiss') as BIGInt) AS DATE_ID,
date_ AS DATE_DESC
FROM
    DATES)a
order by 1

DEMO