PostgreSQL与时间列的时差(以分钟为单位)?

时间:2018-11-11 19:39:23

标签: postgresql

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

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

SELECT DATE_PART('hour','08:56:10':: time-'08:54:55':: time)* 60 +         DATE_PART('minute','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时它才有效。

谢谢!

1 个答案:

答案 0 :(得分: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"
;