I have a mysql table with the following fields:
_id, userId, objectId, timeStamp
I want to calculate the total time an object was interacted with by all users.
I created the query that calculates the time difference between a row and its subsequent row but this doesn’t take into consideration the order of actions by a given user. That query looks like this:
SELECT A._id, A.USERID, A.objectid, A.timeStamp,
SUBSTRING(A.timeStamp, 12, 2) as hourOfDay,
TIME_TO_SEC(TIMEDIFF(B.timeStamp, A.timeStamp)) as diff
FROM InteractionsTBL A
INNER JOIN InteractionsTBL B ON B._id = (A._id + 1) and B.USERID = A.USERID
where A.timeStamp > '2012-04-18 1:00:00'
and A.timeStamp < '2012-04-18 23:59:00'
group by hourOfDay, A.objectid
ORDER BY A._id ASC
How do I calculate the sum total of time interaction with an object by all users grouped by the hour of the day and the objectId?
I ended up writing a pearl script to go in and calculate this difference after the fact. I’m not sure if this is possible in real time with SQL.