邮政系统; select 表示日期和时间查询的整数,在“2021-12-01 00:00:00”和“2021-12-01 23:59:59”之间使用 to_timestamp

Postgres; select integers representing date and time query using to_timestamp between '2021-12-01 00:00:00' and '2021-12-01 23:59:59'

我有包含 unix 时间戳的列 - 表示自纪元以来的秒数的整数。它们看起来像这样:1638888715。我使用 to_timestamp() 函数轻松地将此 int 转换为时间戳,并得到如下所示的输出:2021-12-07 13:51:55+00

我正在尝试 select 24 小时内的数据:2021-12-01 00:00:00 和 2021-12-01 23:59:59

我的查询如下所示:

SELECT to_timestamp(loggeddate), to_timestamp(trxdate), [column a], [column b], [column c], [column d]
FROM [this table]
where [column a] like 'some criteria'
or [column a] like 'some other criteria'
and loggeddate between to_timestamp('2021-12-01 00:00:00') and to_timestamp('2021-12-01 23:59:59')

我得到的错误是:

ERROR:  invalid input syntax for type double precision: "2021-12-01 00:00:00"
LINE 5: and loggeddate between to_timestamp('2021-12-01 00:00:00') a...
                                            ^

有人能解释一下这个显而易见的事情吗?

:::EDIT1:::

感谢您的回复,我现在明白to_timestampto_timestamp(double precision)之间的区别了。整数正在转换为双精度时间戳(我在时间结束时显示时区 +00)。

我的查询的最后一行看起来像:

loggeddate between to_timestamp('2021-12-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and to_timestamp('2021-12-02 00:00:00', 'YYYY-MM-DD HH24:MI:SS')

我收到以下错误:

ERROR:  operator does not exist: integer >= timestamp with time zone
LINE 5: and loggeddate between to_timestamp('2021-12-01 00:00:00', '...
                       ^

我已经设法找到了一个解决方法来满足我的需求;通过将 select 写入不带日期时间过滤器的视图中,整数将转换为可以使用我的 'between' 语句查询的日期时间。

CREATE VIEW trx_data as
SELECT to_timestamp(loggeddate), to_timestamp(trxdate), [column a], [column b], [column c], [column d]
FROM [this table]
where [column a] like 'some criteria'
or [column a] like 'some other criteria'

查询视图:

select * from trx_data
where "logged date" between '2021-12-06 00:00:00' and '2021-12-07 00:00:00'
order by "logged date"

输出如下:

"2021-12-06 00:00:02+00"    "2021-12-05 23:00:01+00"    "THIS EVENT TYPE"   "THIS EVENT NAME"   "THIS AREA" "THIS UNIT"

最好能够一步完成所有这些,而不是在查询之前将数据写入视图,我仍然很感激任何关于使用双精度的指示to_timestamp 来实现一个单一的查询,结果相同。

干杯

EDIT2 - 工作;感谢 SGiux、Adrian 和 Basil

工作查询如下所示:

SELECT to_timestamp(loggeddate), to_timestamp(trxdate), [column a], [column b], [column c], [column d]
FROM [this table]
where [column a] like 'some criteria'
or [column a] like 'some other criteria'
and to_timestamp(loggeddate)
between to_timestamp('2021-12-01 00:00:00')
and to_timestamp('2021-12-02 00:00:00')

PostgreSQL 根本不知道如何读取您作为函数参数传递的字符串。试试这个:

SELECT to_timestamp('2021-12-01 23:59:59', 'YYYY-MM-DD HH24:MI:SS') 

对 EDIT1 的响应

您不能比较两个时间戳之间的整数。试试这个:

to_timestamp(loggeddate) 
    between to_timestamp('2021-12-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and 
    to_timestamp('2021-12-02 00:00:00', 'YYYY-MM-DD HH24:MI:SS')