Ниже для BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT 101 IssueID, TIMESTAMP '2019-08-23 0:25:41' TransTime, 'Peter' User, 'CLAIMED' Status UNION ALL
SELECT 101, '2019-08-23 0:25:44', 'Peter', 'CLAIMED' UNION ALL
SELECT 101, '2019-08-23 0:26:12', 'Peter', 'WAITING' UNION ALL
SELECT 101, '2019-08-23 20:14:13', 'Peter', 'CLAIMED' UNION ALL
SELECT 101, '2019-08-23 20:14:16', 'Peter', 'CLAIMED' UNION ALL
SELECT 101, '2019-08-23 20:14:52', 'Peter', 'WAITING' UNION ALL
SELECT 102, '2019-08-24 8:59:19', 'Miller', 'CLAIMED' UNION ALL
SELECT 102, '2019-08-24 8:59:56', 'Miller', 'CLAIMED' UNION ALL
SELECT 102, '2019-08-24 9:00:09', 'Miller', 'WAITING' UNION ALL
SELECT 102, '2019-08-24 9:00:17', 'Miller', 'CLAIMED' UNION ALL
SELECT 102, '2019-08-24 9:00:20', 'Miller', 'CLAIMED' UNION ALL
SELECT 102, '2019-08-25 21:56:52', 'Miller', 'WAITING'
)
SELECT IssueID, SUM(waiting_time) total_waiting_time
FROM (
SELECT IssueID, TIMESTAMP_DIFF(MAX(TransTime), MIN(TransTime), SECOND) waiting_time
FROM (
SELECT *, COUNTIF(start) OVER(PARTITION BY IssueID ORDER BY TransTime) waiting
FROM (
SELECT *, ('CLAIMED' = status AND IFNULL(LAG(status) OVER(PARTITION BY IssueID ORDER BY TransTime), 'WAITING') = 'WAITING') start
FROM `project.dataset.table`
WHERE status IN ('CLAIMED', 'WAITING')
)
)
GROUP BY IssueID, waiting
)
GROUP BY IssueID
ORDER BY IssueID
с результатом
Row IssueID total_waiting_time
1 101 70
2 102 133045