简体   繁体   English

组内日期之间的SQL差异

[英]SQL difference between dates inside group by

I have this table - 我有这张桌子-

----------------------------------------------
ID  | user   | eventID   | type   | timestamp
----------------------------------------------
1   | 1      | 1         | 3      | 2019-02-08 15:30:00
2   | 1      | 1         | 3      | 2019-02-08 15:31:00
3   | 1      | 1         | 9      | 2019-02-08 15:31:30
4   | 2      | 1         | 3      | 2019-02-08 15:32:00
5   | 2      | 1         | 3      | 2019-02-08 15:32:45
6   | 3      | 1         | 3      | 2019-02-08 15:33:00
7   | 3      | 1         | 9      | 2019-02-08 15:34:00
8   | 3      | 2         | 3      | 2019-02-08 15:35:00
9   | 3      | 2         | 9      | 2019-02-08 15:36:00
10  | 3      | 2         | 3      | 2019-02-08 15:37:00
11  | 3      | 2         | 9      | 2019-02-08 15:37:40

I'm trying to find the time difference between the type 3 and type 9 for each pair of types in each event, for each user. 我正在尝试为每个用户的每个事件中的每个类型对查找类型3和类型9之间的时差。

Output would be - 输出将是-

----------------------------------------------
ID  | user   | eventID   | timeDifference in sec
----------------------------------------------
1   | 1      | 1         | 30
2   | 3      | 1         | 60
3   | 3      | 2         | 60
4   | 3      | 2         | 40

As you can see, each user can have multiple events, and different users can also have the same event. 如您所见,每个用户可以有多个事件,并且不同的用户也可以有相同的事件。 Some users and events have both the types, some don't. 有些用户和事件同时具有两种类型,有些则没有。 There could be multiple type 3 and type 9 events but I want the difference between all the pairs that are closest to each other. 可能有多个类型3和类型9事件,但我想要彼此最接近的所有对之间的差异。 (For each type 9 event, the difference between that and the type 3 event that occurred before that). (对于每个9类事件,此事件与之前发生的3类事件之间的差)。 For each occurrence of type 9 event, there will be a type 3 event before that. 对于每次发生的9类事件,在此之前都会有3类事件。 I also have a separate column that has a different ID for type 9 events, if that's helpful. 我也有一个单独的列,该列具有用于类型9事件的不同ID,如果有帮助的话。 I'm using MYSQL 5.7.23. 我正在使用MYSQL 5.7.23。

How do I go about doing this? 我该怎么做呢?

This will be quite expensive -- it would be simpler in MySQL 8.0. 这将是非常昂贵的-在MySQL 8.0中会更简单。 But you can get the most recent type 3 before each type 9 and then aggregate based on that information: 但是您可以在每个类型9之前获取最新的类型3,然后根据该信息进行汇总:

select user, eventid,
       ( to_seconds(min(t9.timestamp)) - to_seconds(timestamp_3) ) as diff_seconds
from (select t.*,
             (select max(t2.timestamp)
              from t t2
              where t2.user = t.user and t2.eventid = t.eventid and
                    t2.type = 3 and t2.timestamp < t.timestamp
             ) as timestamp_3
      from t
      where t.type = 9
     ) t9
group by user, eventid, timestamp_3;

An index on (user, eventid, type, timestamp) will help performance. 索引(user, eventid, type, timestamp)将有助于提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM