SELECT
sent.push_system,
AVG(timestamp_to_sec(acked._t) - timestamp_to_sec(sent._t)) as avg_ttl,
NTH(100, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p10th,
NTH(200, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p20th,
NTH(300, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p30th,
NTH(400, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p40th,
NTH(500, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p50th,
NTH(600, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p60th,
NTH(700, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p70th,
NTH(800, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p80th,
NTH(900, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p90th,
NTH(950, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p95th,
NTH(990, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p99th,
NTH(999, QUANTILES((TIMESTAMP_TO_USEC(acked._t) - TIMESTAMP_TO_USEC(sent._t))/1e6, 1001)) AS p99_9th,
COUNT(DISTINCT sent.rguid) AS sent,
COUNT(DISTINCT acked.rguid) AS acked
FROM(
SELECT
JSON_EXTRACT_SCALAR(others, '$.push_notification_rguid') AS rguid,
JSON_EXTRACT_SCALAR(others, '$.push_notification_system') AS push_system,
target.device.id AS device_id,
timestamp as _t
FROM
[rounds_events.server_events_20160607]
WHERE
event_name='pushnotif_sent' ) AS sent
JOIN (
SELECT push_notification.platform,
JSON_EXTRACT_SCALAR(others, '$.push_notification_rguid') AS rguid,
JSON_EXTRACT_SCALAR(others, '$.push_notification_system') AS push_system,
target.device.id AS device_id,
timestamp as _t
FROM
[rounds_events.server_events_20160607]
WHERE
event_name='pushnotif_ack') as acked
ON acked.rguid=sent.rguid
WHERE
sent.push_system='ricapi' AND sent.rguid CONTAINS 'trigger_action' AND acked.push_notification.platform='gcm'
GROUP BY
sent.push_system