How to combine two rows and calculate the time difference between two timestamp values in MySQL?
Posted
by Nadar
on Stack Overflow
See other posts from Stack Overflow
or by Nadar
Published on 2010-06-10T18:49:35Z
Indexed on
2010/06/10
19:03 UTC
Read the original article
Hit count: 180
I have a situation that I'm sure is quite common and it's really bothering me that I can't figure out how to do it or what to search for to find a relevant example/solution. I'm relatively new to MySQL (have been using MSSQL and PostgreSQL earlier) and every approach I can think of is blocked by some feature lacking in MySQL.
I have a "log" table that simply lists many different events with their timestamp (stored as datetime type). There's lots of data and columns in the table not relevant to this problem, so lets say we have a simple table like this:
CREATE TABLE log (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(16),
ts DATETIME NOT NULL,
eventtype VARCHAR(25),
PRIMARY KEY (id)
)
Let's say that some rows have an eventtype = 'start' and others have an eventtype = 'stop'. What I want to do is to somehow couple each "startrow" with each "stoprow" and find the time difference between the two (and then sum the durations per each name, but that's not where the problem lies). Each "start" event should have a corresponding "stop" event occuring at some stage later then the "start" event, but because of problems/bugs/crashed with the data collector it could be that some are missing. In that case I would like to disregard the event without a "partner". That means that given the data:
foo, 2010-06-10 19:45, start
foo, 2010-06-10 19:47, start
foo, 2010-06-10 20:13, stop
..I would like to just disregard the 19:45 start event and not just get two result rows both using the 20:13 stop event as the stop time.
I've tried to join the table with itself in different ways, but the key problems for me seems to be to find a way to correctly identify the corresponding "stop" event to the "start" event for the given "name". The problem is exactly the same as you would have if you had table with employees stamping in and out of work and wanted to find out how much they actually were at work. I'm sure there must be well known solutions to this, but I can't seem to find them...
© Stack Overflow or respective owner