简体   繁体   中英

SQL different between two 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

I'm trying to find the time difference between the type 3 and type 9 for each event, for each user.

For example, the output for this table would be -

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

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 the ones closest to each other. And type 9 always happens after type 3. I also have a separate column that has a different ID for type 9 events, if that's helpful. So I'm trying to find the time difference between these 2 types for each event, for each user.

How do I go about doing this?

Edit - I forgot to mention that there can be multiple pairs of type 3 and type 9 events for each event, for each user. SO I want the difference of all these pairs, that are next to each other, not just one difference per group by.

For 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

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

edit - I asked a difference question with my edit. The original question here was answered already.

This answers the original version of the question:

select user, eventid,
       (to_seconds(min(case when type = 9 then timestamp end)),
        to_seconds(max(case when type = 3 then timestamp end))
       ) as diff_seconds
from t
group by user, eventid;

This returns the difference between the earliest "9" timestamp and the latest "3" timestamp.

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