In my sample data table below, all datatypes are varchar(255).
| event | timestamp | traceid | | accepted | 2016-01-16T09:35:48.653418931Z | AIP1452936948-44467 | | published_event | 2016-01-16T09:35:48.659767348Z | AIP1452936948-44467 |
Here is the expected output:
| traceid | duration | | AIP1452936948-44467 | 108 |
And the code I would like reviewed...
select traceid, bUnixMS - aUnixMS as duration
from (
select
a.traceid,
CONVERT(UNIX_TIMESTAMP(STR_TO_DATE(a.mydatetime,'%Y-%m-%d %H:%i:%s')), SIGNED) as aUnix,
CONVERT(UNIX_TIMESTAMP(STR_TO_DATE(b.mydatetime,'%Y-%m-%d %H:%i:%s')), SIGNED) as bUnix,
(CONVERT(UNIX_TIMESTAMP(STR_TO_DATE(a.mydatetime,'%Y-%m-%d %H:%i:%s')), SIGNED) + (CONVERT(LEFT(a.ms, 3), SIGNED) / 1000)) * 1000 as aUnixMS,
(CONVERT(UNIX_TIMESTAMP(STR_TO_DATE(b.mydatetime,'%Y-%m-%d %H:%i:%s')), SIGNED) + (CONVERT(LEFT(b.ms, 3), SIGNED) / 1000)) * 1000 as bUnixMS,
CONVERT(LEFT(a.ms, 3), SIGNED) as aMS,
CONVERT(LEFT(b.ms, 3), SIGNED) as bMS
from (
select
event,
traceid,
CONCAT(SUBSTRING_INDEX(timestamp, 'T', 1), ' ', SUBSTRING_INDEX(SUBSTRING_INDEX(timestamp, 'T', -1), '.', 1)) as mydatetime,
SUBSTRING_INDEX(SUBSTRING_INDEX(timestamp, 'T', -1), '.', -1) as ms
from applogs
where event = "accepted"
) a
join (
select
event,
traceid,
CONCAT(SUBSTRING_INDEX(timestamp, 'T', 1), ' ', SUBSTRING_INDEX(SUBSTRING_INDEX(timestamp, 'T', -1), '.', 1)) as mydatetime,
SUBSTRING_INDEX(SUBSTRING_INDEX(timestamp, 'T', -1), '.', -1) as ms
from applogs
where event = "published_event"
) b on a.traceid = b.traceid
) as x
traceid
witht1.event = 'accepted'
ANDt2.event = 'published'
? \$\endgroup\$traceid
andevent
? \$\endgroup\$