基于两组的Postgres窗口函数计算

时间:2018-05-03 18:43:33

标签: postgresql dense-rank

我在使用窗口函数进行简单查询时遇到了困难。我的问题来自于我需要第二行作为等式的一部分。

这是我的基本选择* From table events。

  id   |          type          | event_id | event_user |     event_time                                                                                                                                                                                   
-------+------------------------+----------+------------+---------------------                                                                                                                                                                             
 42047 | flex1                  |     4742 |          8 | 2018-04-29 06:25:03                                                                                                                                                                              
 42046 | flex1                  |     4742 |          8 | 2018-04-29 06:25:17                                                                                                                                                                              
 42042 | flex1                  |     4742 |          8 | 2018-04-29 06:57:00                                                                                                                                                                              
 42043 | flex1                  |     4742 |          8 | 2018-04-29 08:20:00                                                                                                                                                                              
 42045 | flex1                  |     4742 |          8 | 2018-04-29 08:34:54                                                                                                                                                                              
 42044 | flex2                  |     4742 |          8 | 2018-04-29 08:38:02    

我的目标是获得两个事件之间的时差,其中下一个事件是上一个事件之后的第一个事件。像这样....

  id   |          type          | event_id | event_user |     event_time                                                                                                                                                                                   
-------+------------------------+----------+------------+---------------------                                                                                                                                                                             
 42047 | flex1                  |     4742 |          8 | 2018-04-29 06:25:03                                                                                                                                                                              
 42046 | flex1                  |     4742 |          8 | 2018-04-29 06:25:17                                                                                                                                                                              

-- Need difference between event_time of (id 42046) and event_time of (id 42047)                                                                                                                                                                           

 42042 | flex1                  |     4742 |          8 | 2018-04-29 06:57:00                                                                                                                                                                              
 42043 | flex1                  |     4742 |          8 | 2018-04-29 08:20:00                                                                                                                                                                              


-- Need difference between event_time of (id 42043) and event_time of (id 42042)                                                                                                                                                                           

 42045 | flex1                  |     4742 |          8 | 2018-04-29 08:34:54                                                                                                                                                                              
 42044 | flex2                  |     4742 |          8 | 2018-04-29 08:38:02                                                                                                                                                                              


-- Need difference between event_time of (id 42044) and event_time of (id 42045) 

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

如果您希望events排序event_time进行计算,您可以先计算下一行的差异,然后取结果的每一行:

SELECT id, prev_id, diff
FROM (SELECT id,
             lag(id) OVER w AS prev_id,
             lag(event_time) OVER w - event_time AS diff,
             row_number() OVER w
      FROM events
      WINDOW w AS (ORDER BY event_time DESC)
     ) q
WHERE row_number % 2 = 0;

  id   | prev_id |   diff   
-------+---------+----------
 42045 |   42044 | 00:03:08
 42042 |   42043 | 01:23:00
 42047 |   42046 | 00:00:14
(3 rows)