【问题标题】:Find difference between timestamps in seconds in PostgreSQL在 PostgreSQL 中以秒为单位查找时间戳之间的差异
【发布时间】:2012-12-10 20:24:55
【问题描述】:

我在PostgreSQL 8.3 中有一个表格,有 2 个timestamp 列。我想在几秒钟内得到这些timestamps 之间的区别。您能帮我完成这项工作吗?

TableA
(
  timestamp_A timestamp,
  timestamp_B timestamp
)

我需要在几秒钟内得到类似(timestamo_B - timestamp_A) 的东西(不仅仅是秒之间的差异,它应该包括小时、分钟等)

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    试试:

    SELECT EXTRACT(EPOCH FROM (timestamp_B - timestamp_A))
    FROM TableA
    

    详情请看:EXTRACT

    【讨论】:

    • 非常感谢您的回答。有效 !!!在上面的查询中,我们缺少一个右括号。但我已经想通了。非常感谢您的快速回复。
    【解决方案2】:
    select age(timestamp_A, timestamp_B)
    

    回答伊戈尔的评论:

    select age('2013-02-28 11:01:28'::timestamp, '2011-12-31 11:00'::timestamp);
                  age              
    -------------------------------
     1 year 1 mon 28 days 00:01:28
    

    【讨论】:

    • 它不会完成这项工作。它将Subtract arguments, producing a "symbolic" result that uses years and months。它不会在几秒钟内给出差异。
    • @Igor 更新了包括秒数在内的结果。 OP 不仅想要秒,还想要分钟、小时等
    • 如果我理解正确的话,他想要to get the difference between these timestamps in seconds。而it should include hours, minutes etc 意味着它必须像10:25:30 - 10:15:25 = 605 seconds 一样完全不同。我的猜测 - 他使用了EXTRACT(SECONDS FROM ...) 并得到了10:25:30 - 10:15:25 = 5 seconds
    • @Igor 这不是很清楚,但现在你这么说我想你可能是对的。
    • @Clodoaldo:我需要 Igor 提到的输出。我需要几秒钟内的全部差异。
    【解决方案3】:
    SELECT (cast(timestamp_1 as bigint) - cast(timestamp_2 as bigint)) FROM table;
    

    如果有人在使用 extract 时遇到问题。

    【讨论】:

    • 它也不适用于带时区的时间戳。
    猜你喜欢
    • 2019-01-27
    • 2012-05-01
    • 2019-01-28
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多