Google-Bigquery

如何從列中查找連續的非零值

  • December 2, 2020

我有一張如下表

user  timestamp            counts   
xyz   01-01-2020 00:05:00   12
xyz   01-01-2020 00:10:00   11
xyz   01-01-2020 00:15:00   45
xyz   01-01-2020 00:20:00   0
xyz   01-01-2020 00:25:00   0
xyz   01-01-2020 00:30:00   13
xyz   01-01-2020 00:35:00   12
xyz   01-01-2020 00:40:00   0

我想根據計數列中的最大連續值(非零)值對其進行切片,並找出該持續時間內的開始時間戳、結束時間戳和總計數

starttime            endtime               total_count
01-01-2020 00:05:00  01-01-2020 00:15:00   68

如何使用 BigQuery 實現這一目標

測試類似的東西

WITH cte AS ( SELECT *, SUM(CASE WHEN counts=0 THEN 1 ELSE 0 END) OVER (ORDER BY timestamp) grp
             FROM sourcetable )
SELECT MIN(timestamp) start_time, MAX(timestamp) end_time, SUM(counts) total_count
FROM cte
WHERE grp = 0

展示小提琴(使用 MySQL 8)

引用自:https://dba.stackexchange.com/questions/280744