我有包含 unix 时间戳的列 - 表示自纪元以来的秒数的整数。它们看起来像这样:1638888715
。我正在使用该to_timestamp()
函数轻松地将这个 int 转换为时间戳,并得到如下所示的输出:2021-12-07 13:51:55+00
我正在尝试在 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_timestamp
和to_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', '...
^
我设法找到了一个可以让我得到我想要的东西的工作;通过将选择写入没有日期时间过滤器的视图,整数被转换为可以使用我的“介于”语句查询的日期时间。
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')