同一列中日期的总间隔


10

如何最好地总结交织行之间同一列中日期范围的差异?我有一个Datetime列,想计算行之间的差异。我想以秒为单位。这个问题不是关于如何获得两个时间戳之间的差异,而是更多地集中于如何最有效地计算同一表的行之间的差异。在我的情况下,每一行都有一个datetime偶数类型,该类型将两行逻辑地链接在一起。

详细信息与如何对开始和结束事件类型进行分组有关。(Andriy M的问题)“应该”是连续的开始和结束。如果起点没有后续终点,则应将其排除在总和之外。移至下一个“开始”以查看其是否结束。仅应将连续的开始-结束对添加到总秒数之和。

在PostgreSQL 9.x中工作...

表中的示例数据;

eventtype, eventdate
START, 2015-01-01 14:00
END, 2015-01-01 14:25
START, 2015-01-01 14:30
END, 2015-01-01 14:43
START, 2015-01-01 14:45
END, 2015-01-01 14:49
START, 2015-01-01 14:52
END, 2015-01-01 14:55

注意,所有开始日期和结束日期将是连续的。

这是我的第一次尝试。似乎正在工作。

SELECT 
-- starts.*
SUM(EXTRACT(EPOCH FROM (eventdate_next - eventdate))) AS duration_seconds
FROM
( 
    WITH x AS (
        SELECT *, dense_rank() OVER (ORDER BY eventdate) AS rnk
        FROM   table
        AND eventdate > '2015-01-01 00:00:00.00'
        AND eventdate < '2016-01-01 23:59:59.59' 
        )
    SELECT x.eventdate, x.eventtype, y.eventdate AS eventdate_next,  y.eventtype AS eventtype_next
    FROM   x
    LEFT   JOIN (SELECT DISTINCT eventdate, eventtype, rnk FROM x) y ON y.rnk = (x.rnk + 1)
    ORDER  BY x.eventdate
) starts
WHERE
eventtype = 'START'   
GROUP BY eventtype 

我的第一次尝试是基于stackoverflow Postgres 9.1的一个很好的例子 -获得下一个价值

注意; 您可以注释GROUP BY和SUM并取消注释起始点*,以获取记录在总和中的每个持续时间的记录。

Answers:


10

您可以使用LEAD解析函数来获得下一行是eventtypeeventdate旁边的当前行的数据:

SELECT
  eventtype,
  eventdate,
  LEAD(eventtype) OVER (ORDER BY eventdate) AS nexttype,
  LEAD(eventdate) OVER (ORDER BY eventdate) AS nextdate
FROM
  atable
WHERE
      eventdate >= '2015-01-01 00:00:00.00'
  AND eventdate <  '2016-01-01 23:59:59.59'

使用上面的查询作为派生表,您可以进一步过滤输出eventtype = 'START' AND nexttype = 'END'并获得总差:

SELECT
  SUM(EXTRACT(EPOCH FROM (nextdate - eventdate))) AS duration_seconds
FROM
  (
    SELECT
      eventtype,
      eventdate,
      LEAD(eventtype) OVER (ORDER BY eventdate) AS nexttype,
      LEAD(eventdate) OVER (ORDER BY eventdate) AS nextdate
    FROM
      atable
    WHERE
          eventdate >= '2015-01-01 00:00:00.00'
      AND eventdate <  '2016-01-01 23:59:59.59'
  ) AS s
WHERE
      eventtype = 'START'
  AND nexttype  = 'END'
;

作为一个细微的变化,您可以将子查询实现为CTE:

WITH cte AS
  (
    SELECT
      eventtype,
      eventdate,
      LEAD(eventtype) OVER (ORDER BY eventdate) AS nexttype,
      LEAD(eventdate) OVER (ORDER BY eventdate) AS nextdate
    FROM
      atable
    WHERE
          eventdate >= '2015-01-01 00:00:00.00'
      AND eventdate <  '2016-01-01 23:59:59.59'
  )
SELECT
  SUM(EXTRACT(EPOCH FROM (nextdate - eventdate))) AS duration_seconds
FROM
  cte
WHERE
      eventtype = 'START'
  AND nexttype  = 'END'
;

这种重写可能会对性能产生影响,因为与派生表不同,CTE在PostgreSQL中得以实现。测试应该显示出是否存在差异,如果存在差异,哪种选择更适合您。


安德里,谢谢!我将尝试CTE版本,并了解它的帮助。
史密斯C史密斯
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.