How to find the difference between two dates for the same id in MySQL? -


i have mysql database containing ticket id, create time of ticket , create time of each interaction between company operator , client.

i total awaiting time on client , company side same ticket id.

id      id_ticket      id_sender      time

1       123             1               2010-04-26 03:32:44

2       159             1               2010-04-26 03:40:44

3       983             1               2010-04-26 04:44:26

4       159             2               2010-04-26 05:12:35

5       159             1               2010-04-26 06:25:45

6       123             2               2010-04-26 08:46:12

7       159             2               2010-04-26 09:36:25

8       123             1               2010-04-26 12:48:39

id_side either company or client answer.

is there way in mysql ? thanks.

[as example, total awaiting time customer (id_sender = 1) ticket #123 (2010-04-26 08:46:12 - 2010-04-26 03:32:44) + (2010-04-26 12:48:39 - 2010-04-26 08:46:12) = 18808 + 14547 = 33355 sec.]

join table itself, join condition joined row have previous highest id same ticket_id:

select sum(to_seconds(a.time) - to_seconds(b.time)) total_wait ticket join ticket b on b.id = (     select max(id)     ticket     id < a.id     , id_ticket = a.id_ticket) a.id_ticket = 123 

see live demo on sqlfiddle using data, output: 33355


you all wait times in similar way:

select a.id_ticket, sum(to_seconds(a.time) - to_seconds(b.time)) total_wait ticket join ticket b on b.id = (     select max(id)     ticket     id < a.id     , id_ticket = a.id_ticket) group 1 

see sqlfiddle that


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -