BigQuery-计算来自IoT设备的活动块

时间:2019-12-03 19:28:33

标签: google-bigquery

以下是示例数据:

create table activity as
select '2019-11-06T13:05:07' as datetime, 1 as active union all
select '2019-11-06T13:05:08' as datetime, 1  as active union all
select '2019-11-06T13:05:09' as datetime, 1  as active union all
select '2019-11-06T13:05:10' as datetime, 1  as active union all
select '2019-11-06T13:05:11' as datetime, 0  as active union all
select '2019-11-06T13:05:12' as datetime, 0  as active union all
select '2019-11-06T13:05:13' as datetime, 0  as active union all
select '2019-11-06T13:05:14' as datetime, 0  as active union all
select '2019-11-06T13:05:15' as datetime, 0  as active union all
select '2019-11-06T13:05:16' as datetime, 1  as active union all
select '2019-11-06T13:05:17' as datetime, 1  as active union all
select '2019-11-06T13:05:18' as datetime, 1  as active union all
select '2019-11-06T13:05:19' as datetime, 1  as active union all
select '2019-11-06T13:05:20' as datetime, 1  as active union all
select '2019-11-06T13:05:21' as datetime, 0  as active union all
select '2019-11-06T13:05:22' as datetime, 0 as active;

我正在寻找可计算活动块的查询。 在上面的示例中,我们有2个活动块。

  
      
  1. 13:05:07-13:05:10
  2.   
  3. 13:05:16-13:05:20
  4.   

1 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
SELECT ROW_NUMBER() OVER(ORDER BY grp) block_num, 
  MIN(datetime) activity_start, 
  MAX(datetime) activity_end  
FROM (
  SELECT *, COUNTIF(flag) OVER(ORDER BY datetime) grp
  FROM (
    SELECT *, active != LAG(active) OVER(ORDER BY datetime) flag
    FROM `project.dataset.activity`
  )
)
WHERE active = 1
GROUP BY grp   

如果要应用于您的问题的样本数据-结果为

Row block_num   activity_start          activity_end     
1   1           2019-11-06T13:05:07     2019-11-06T13:05:10  
2   2           2019-11-06T13:05:16     2019-11-06T13:05:20