【问题标题】:Get delta of current date and column's timestamp - does not exist: double precision - timestamp without time zone获取当前日期和列时间戳的增量 - 不存在:双精度 - 没有时区的时间戳
【发布时间】:2020-09-16 17:39:39
【问题描述】:

列 created_at, modified_at 类型 timestamp 没有时区

以毫秒为单位在此列中获取增量:

select extract(milliseconds from modified_at - created_at) as delta_millsfrom shop_order

很好。现在我想获取增量当前日期和列 created_at

我试试这个:

select id, extract(milliseconds from (EXTRACT(EPOCH FROM current_timestamp) * 1000) - created_at) as detla_mills from shop_order 

但报错

:
ERROR:  operator does not exist: double precision - timestamp without time zone
LINE 1: ...om (EXTRACT(EPOCH FROM current_timestamp) * 1000) - created_...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
SQL state: 42883
Character: 85

【问题讨论】:

  • 只看第二个表达式的语法,我对从current_timestamp提取的EPOCH应用的* 1000很怀疑。我怀疑它会导致正确的timestamp 类型。鉴于抱怨双精度减去时间戳的错误消息,我猜想乘法会给你一个double,你不能直接从中减去timestamp。也许有办法将其转换回timestamp
  • @RobParker 这项工作:选择 id, created_at, extract(milliseconds from (CURRENT_TIMESTAMP) - created_at) 作为 shop_order 的增量
  • 我应该把我的评论作为答案然后获得信用吗?我没有,因为我不特别了解 PostgreSQL,也无法给出有效的示例解决方案......只是意识到错误在语法中抱怨的内容的提示。

标签: postgresql-9.5


【解决方案1】:

从错误文本和检查失败表达式的语法来看,问题在于EXTRACT(EPOCH FROM current_timestamp) 可能不是timestamp 类型,并且肯定将其乘以* 1000 不能明智地导致@ 987654324@ 类型(什么日期是(2020 年 9 月 16 日)乘以 1000,嗯?)。根据错误,它是一个double precision,你不能从中减去一个timestamp

你需要从current_timestamp中减去一些基础timestamp值,然后将其转换为您想要的另一种类型或部分,或者以同样的方式将基础和current_timestamp转换为另一种可以直接的类型减去增量(例如将提取的EPOCH 转换回timestamp 开头的EPOCH

【讨论】:

    猜你喜欢
    • 2013-03-10
    • 1970-01-01
    • 2010-12-31
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2017-12-01
    • 2020-01-02
    相关资源
    最近更新 更多