I have this query,
SELECT
testTimes.reportId,
testTimes.userID,
-- diff,
SUM(diff) AS total
FROM
(
SELECT
start_log.reportID,
start_log.userID,
start_log.testID,
MAX(start_log.eventDateTime) AS start_time,
end_log.eventDateTime AS end_time,
TIMESTAMPDIFF(MINUTE,
MAX(start_log.eventDateTime),
end_log.eventDateTime) AS diff
FROM
testtracker_event AS start_log
INNER JOIN testtracker_event AS end_log ON
( start_log.userID = end_log.userID
AND start_log.reportID = end_log.reportID
AND start_log.testID = end_log.testID
AND end_log.eventDateTime > start_log.eventDateTime)
WHERE
(start_log.eventType = 'start'
OR start_log.eventType = 'resume')
AND (end_log.eventType = 'finish'
OR end_log.eventType = 'pause')
AND start_log.reportID = '20466'
GROUP BY
start_log.userID,
start_log.testID,
start_log.eventDateTime,
start_log.reportID
ORDER BY
start_time DESC) testTimes
Which when run against this data set;
|reportID|testID|eventDateTime|userID|eventType|workstation|note|
|--------|------|-------------|------|---------|-----------|----|
|20466|1|2018-04-26 14:41:42|msharpe|start| | |
|20466|1|2018-04-26 15:17:10|msharpe|finish| | |
|20466|1|2018-04-26 15:17:20|msharpe|reset| |"MIS-CLICK"|
|20466|1|2018-04-26 15:17:21|msharpe|start| | |
|20466|1|2018-04-27 08:11:02|msharpe|finish| | |
|20466|2|2018-04-26 14:41:43|msharpe|start| | |
|20466|2|2018-04-27 08:11:02|msharpe|finish| | |
|20466|3|2018-04-26 14:42:15|msharpe|start| | |
|20466|3|2018-04-26 15:17:23|msharpe|finish| | |
|20466|4|2018-04-26 15:19:25|msharpe|start| | |
|20466|4|2018-04-26 15:34:59|msharpe|finish| | |
|20466|5|2018-04-26 14:42:21|msharpe|start| | |
|20466|5|2018-04-26 15:17:29|msharpe|finish| | |
|20466|7|2018-04-26 16:32:57|msharpe|start| | |
|20466|7|2018-04-27 08:11:03|msharpe|finish| | |
|20466|8|2018-04-26 15:38:29|msharpe|markna| | |
|20466|10|2018-04-27 12:05:21|msharpe|start| | |
|20466|10|2018-04-27 12:05:22|msharpe|finish| | |
|20466|16|2018-04-26 14:42:17|msharpe|start| | |
|20466|16|2018-04-26 15:17:28|msharpe|finish| | |
|20466|40|2018-04-26 14:42:17|msharpe|start| | |
|20466|40|2018-04-26 15:17:26|msharpe|finish| | |
|20466|50|2018-04-27 12:05:22|msharpe|start| | |
|20466|50|2018-04-27 12:05:23|msharpe|finish| | |
|20466|52|2018-04-27 08:50:54|msharpe|start| | |
|20466|52|2018-04-27 10:00:27|msharpe|finish| | |
|20466|53|2018-04-26 15:58:56|msharpe|start| | |
|20466|53|2018-04-26 16:32:56|msharpe|finish| | |
|20466|54|2018-04-26 15:38:24|msharpe|markna| | |
RESULT - 20466 msharpe 3293
I currently get 3,293 minutes as a result, and even with concurrent scheduling and 2 cases where it looks like an even was signed on overnight, I would expect the results to be VERY Roughly 1845
I can see there is some duplication happening and I would guess it has something to do with the join
however I lack the experience or knowledge to get the right answer, in this case, answers I have found, I have trouble correlating to this situation.
While I understand some might be tempted to cite an older one of my questions How would I update this MySQL query to get total elapsed time from start, stop, pause and resume events and this question Calculating difference on datetime row betwen rows on the same table as duplicates, I am asking about improving the existing query to not have the compound effect, and/or minimize duplication.
SQLFiddle for good measure, http://www.sqlfiddle.com/#!9/0dd34d/1