在 Bigquery 中获取特定事件类型的第一行?

Aks*_*pte 2 sql google-bigquery

Row   EventType   CloudId           ts
1     stop        5201156607311872  2018-07-07 12:25:21 UTC  
2     start       5201156607311872  2018-07-07 12:27:39 UTC  
3     start       5201156607311872  2018-07-07 12:28:15 UTC  
4     stop        5738776789778432  2018-07-07 12:28:54 UTC  
5     stop        5201156607311872  2018-07-07 12:30:30 UTC  
6     stop        5738776789778432  2018-07-07 12:37:45 UTC  
7     stop        5738776789778432  2018-07-07 12:40:52 UTC
Run Code Online (Sandbox Code Playgroud)

我有一个如上所述的表结构。我只想EventType在行更改之前过滤第一个事件。即row 2row 3有相同的EventType,我需要row 3从表中删除。row 4,5,6,7有同样的EventType,我想保留row 4和删除row 5,6,7

Gor*_*off 5

使用lag()

select t.*
from (select t.*,
             lag(eventtype) over (order by row) as prev_eventtype
      from t
     ) t
where prev_eventtype is null or prev_eventtype <> eventtype;
Run Code Online (Sandbox Code Playgroud)