Sql 是否在Bigquery中获取特定事件类型的第一行?
我有一个表结构如上所述。我只想筛选行的EventType更改之前的第一个事件。i、 e第2行和第3行具有相同的EventType,我需要从表中删除第3行。第4,5,6,7行具有相同的EventType,我希望保留第4行并删除第5,6,7行 您可以使用SELECT语句来隐藏不需要的行:Sql 是否在Bigquery中获取特定事件类型的第一行?,sql,google-bigquery,Sql,Google Bigquery,我有一个表结构如上所述。我只想筛选行的EventType更改之前的第一个事件。i、 e第2行和第3行具有相同的EventType,我需要从表中删除第3行。第4,5,6,7行具有相同的EventType,我希望保留第4行并删除第5,6,7行 您可以使用SELECT语句来隐藏不需要的行: select Row, EventType, CloudId, ts from ( select Row, EventType, CloudId, ts, row_number() over (partition
select
Row,
EventType,
CloudId,
ts
from
(
select
Row,
EventType,
CloudId,
ts,
row_number() over (partition by EventType order by CloudId,Row) as rnk
from table_name
)evnt where rnk=1
您可以使用SELECT语句来隐藏不需要的行:
select
Row,
EventType,
CloudId,
ts
from
(
select
Row,
EventType,
CloudId,
ts,
row_number() over (partition by EventType order by CloudId,Row) as rnk
from table_name
)evnt where rnk=1
使用滞后:
使用滞后:
下面是BigQuery标准SQL
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;
下面是BigQuery标准SQL
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;
谢谢你的回复。我可以知道BigQuery中行运算符的等价形式吗。查询抛出错误:`Name row not found in t at[3:9]`感谢您的回复。我可以知道BigQuery中行运算符的等价形式吗。查询抛出错误:`Name row not found in t at[3:9]`谢谢Mikhail!上述解决方案有效。你总是在那里为我的BQ困难:谢谢米哈伊尔!上述解决方案有效。你总是在我的BQ问题上:
#standardSQL
SELECT * EXCEPT(prev_eventtype) FROM (
SELECT *, LAG(eventtype) OVER (ORDER BY ts) AS prev_eventtype
FROM `project.dataset.table`
)
WHERE prev_eventtype IS NULL OR prev_eventtype <> eventtype
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'stop' EventType, 5201156607311872 CloudId, TIMESTAMP '2018-07-07 12:25:21 UTC' ts UNION ALL
SELECT 'start', 5201156607311872, '2018-07-07 12:27:39 UTC' UNION ALL
SELECT 'start', 5201156607311872, '2018-07-07 12:28:15 UTC' UNION ALL
SELECT 'stop', 5738776789778432, '2018-07-07 12:28:54 UTC' UNION ALL
SELECT 'stop', 5201156607311872, '2018-07-07 12:30:30 UTC' UNION ALL
SELECT 'stop', 5738776789778432, '2018-07-07 12:37:45 UTC' UNION ALL
SELECT 'stop', 5738776789778432, '2018-07-07 12:40:52 UTC'
)
SELECT * EXCEPT(prev_eventtype) FROM (
SELECT *, LAG(eventtype) OVER (ORDER BY ts) AS prev_eventtype
FROM `project.dataset.table`
)
WHERE prev_eventtype IS NULL OR prev_eventtype <> eventtype
EventType CloudId ts
stop 5201156607311872 2018-07-07 12:25:21 UTC
start 5201156607311872 2018-07-07 12:27:39 UTC
stop 5738776789778432 2018-07-07 12:28:54 UTC