简体   繁体   中英

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.

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. (For each type 9 event, the difference between that and the type 3 event that occurred before that). For each occurrence of type 9 event, there will be a type 3 event before that. I also have a separate column that has a different ID for type 9 events, if that's helpful. I'm using MYSQL 5.7.23.

How do I go about doing this?

This will be quite expensive -- it would be simpler in MySQL 8.0. But you can get the most recent type 3 before each type 9 and then aggregate based on that information:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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