在 Postgresql 上配对顺序事件

Pairing Sequential Events on Postgresql

我们正在 table 上记录用户在我们的 iPad 应用程序上进行的主要操作流程。每个流都有一个开始(标记为已开始)和一个标记为已取消或已完成的结束,并且不应有任何重叠事件。

用户的一组已开始、已取消或已完成的流程如下所示:

user_id             timestamp                   event_text      event_num
info@cafe-test.de   2016-10-30 00:08:00.966+00  Flow Started    0
info@cafe-test.de   2016-10-30 00:08:15.58+00   Flow Cancelled  2
info@cafe-test.de   2016-10-30 00:08:15.581+00  Flow Started    0
info@cafe-test.de   2016-10-30 00:34:44.134+00  Flow Finished   1
info@cafe-test.de   2016-10-30 00:42:26.102+00  Flow Started    0
info@cafe-test.de   2016-10-30 00:42:49.276+00  Flow Cancelled  2
info@cafe-test.de   2016-10-30 00:42:49.277+00  Flow Started    0
info@cafe-test.de   2016-10-30 00:59:47.337+00  Flow Cancelled  2
info@cafe-test.de   2016-10-30 00:59:47.337+00  Flow Started    0
info@cafe-test.de   2016-10-30 00:59:47.928+00  Flow Cancelled  2

我们要计算已取消和已完成的流程平均持续多长时间。为此,我们需要将事件 Started 与 Canceled 或 Finished 配对。以下代码可以做到这一点,但无法解决我们遇到的以下数据质量问题:

我们得到这个结果:

user_id             start                       end                         action
info@cafe-test.de   2016-10-30 00:08:00.966+00  2016-10-30 00:08:15.58+00   2
info@cafe-test.de   2016-10-30 00:08:15.581+00  2016-10-30 00:34:44.134+00  1
info@cafe-test.de   2016-10-30 00:42:26.102+00  2016-10-30 00:42:49.276+00  2
info@cafe-test.de   2016-10-30 00:42:49.277+00  NULL                        NULL
info@cafe-test.de   2016-10-30 00:59:47.337+00  2016-10-30 00:59:47.337+00  2
info@cafe-test.de   NULL                        2016-10-30 00:59:47.928+00  2

但我们应该得到这个:

user_id             start                       end                         action
info@cafe-test.de   2016-10-30 00:08:00.966+00  2016-10-30 00:08:15.58+00   2
info@cafe-test.de   2016-10-30 00:08:15.581+00  2016-10-30 00:34:44.134+00  1
info@cafe-test.de   2016-10-30 00:42:26.102+00  2016-10-30 00:42:49.276+00  2
info@cafe-test.de   2016-10-30 00:42:49.277+00  2016-10-30 00:59:47.337+00  2
info@cafe-test.de   2016-10-30 00:59:47.337+00  2016-10-30 00:59:47.928+00  2

我需要如何更改代码才能正确配对?

select      user_id       
           ,"start"                       
           ,"end"                         
           ,"action"

from       (select      user_id
                       ,timestamp                 as "start"
                       ,lead (event_num)   over w as "action"
                       ,lead ("timestamp") over w as "end"
                       ,event_num

            from        tracks t

            window      w as (partition by user_id order by "timestamp",event_num desc)
            ) t

where       t.event_num = 0
;