SELECT owner_id,
ts,
if((owner_id != neighbor(owner_id, 1)) OR (user_agent != neighbor(user_agent, 1)), 0,
neighbor(ts, 1)) AS LEAD,
platform_id,
user_agent,
operation
FROM (
SELECT owner_id,
ts,
platform_id,
user_agent,
date,
operation
FROM events
WHERE (operation LIKE 'app_pageview_%')
AND (date = '2020-06-07')
AND ((ts >= 1591488000000) AND (ts <= 1591574399000))
ORDER BY owner_id ASC,
user_agent ASC,
ts ASC
) AS events
INNER JOIN
(
SELECT *
FROM (
SELECT owner_id,
platform_id,
user_agent,
min(ts) AS MIN
FROM events
WHERE (operation LIKE 'app_pageview_%')
AND (date = '2020-06-07')
AND ((ts >= 1591488000000) AND (ts <= 1591574399000))
GROUP BY owner_id,
platform_id,
user_agent
ORDER BY owner_id ASC,
user_agent ASC
)
ORDER BY owner_id ASC,
user_agent ASC
) AS min_events
ON (min_events.owner_id = events.owner_id) AND (min_events.platform_id = events.platform_id) AND
(min_events.user_agent = events.user_agent)
WHERE ((ts >= 1591488000000) AND (ts <= 1591574399000))
AND (date = '2020-06-07')
AND (operation LIKE 'app_pageview_%')
ORDER BY owner_id ASC,
user_agent ASC,
ts ASC;