【问题标题】:PostgreSQL time difference in minutes from time column?PostgreSQL与时间列的时间差(以分钟为单位)?
【发布时间】:2018-11-11 19:39:23
【问题描述】:

我想使用 PostgreSQL(电话)计算持续时间(以分钟为单位)。

-- 08:54:55 和 08:56:10 之间的差异(仅时间)

SELECT DATE_PART('hour', '08:56:10'::time - '08:54:55'::time) * 60 + DATE_PART('分钟', '08:56:10'::time - '08:54:55'::time); -- 结果:1

如何修改它以适用于整个数据集?开始时间(只有小时和分钟,秒总是 00)和结束时间在同一列 Event_Time 中,按 Correlation_ID 分组。

Event_Time  Customer_ID Case_ID Event_ID    Correlation_ID  Line_ID Internal_ID
10:11:00            id1     1       start       sd34        l3      456
10:13:00            id5     8       stop        rt56        l3      456
10:15:00            id4     3       start       tg84        l7      567
10:17:00            id7     5       start       ty69        l4      678
10:21:00            id5     5       stop        rt56        l7      678
10:31:00            id1     1       stop        sd34        l4      567

我尝试了这个解决方案,但它只有在我指定时间http://www.sqlines.com/postgresql/how-to/datediff 时才有效。

谢谢!

【问题讨论】:

  • 请为您的问题添加更多详细信息。阅读本文以获取有关如何提出更好的 sql 问题的提示:meta.stackoverflow.com/questions/271055/…
  • 谢谢!刚刚在我的问题中添加了更多信息。
  • 我假设列"Event_Time" 是一个时间列(不是某种字符串)

标签: postgresql


【解决方案1】:

也许有这样的自我加入:

SELECT
  start."Correlation_ID",
  extract(epoch from stop."Event_time" - start."Event_time")/60
FROM
  some_table AS start
JOIN
  some_table AS stop
  ON start."Correlation_ID"=finish."Correlation_ID"
  AND stop."Event_ID"='stop'
WHERE 
  start."Event_ID"='start'
;

或与这样的团体:

SELECT
  "Correlation_ID",
  extract(epoch from 
     max(CASE "Event_ID" WHEN 'stop' THEN "Event_time" END ) 
    -min(CASE "Event_ID" WHEN 'start' THEN "Event_time" END ) 
    ) /60
FROM
  some_table
JOIN
  grouP BY "Correlation_ID"
;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2020-06-08
    相关资源
    最近更新 更多