简体   繁体   English

Bigquery/ SQL:同一组中的序列

[英]Bigquery/ SQL: sequence in same group

I have a question in regards to finding the sequence between 2 page types using sql:我有一个关于使用 sql 查找两种页面类型之间的序列的问题:

Sample data we have:我们拥有的样本数据:

SessionID会话ID Page Type页面类型
123 123 account帐户
123 123 raw生的
123 123 plp plp
123 123 pdp pdp
123 123 account帐户
123 123 plp plp
123 123 sample样本
123 123 pdp pdp
123 123 plp plp
123 123 pdp pdp
123 123 raw生的

Ideally output will look like below, only to include records when pdp is clicked right after plp理想情况下,output 如下所示,仅包含在 plp 之后单击 pdp 时的记录

SessionID会话ID Page Type页面类型
123 123 plp plp
123 123 pdp pdp
123 123 plp plp
123 123 pdp pdp

In order to operate based on order - you must have something to order on为了根据订单进行操作 - 你必须有一些东西要订购
Usually it is timestamp column or position or something else通常是时间戳列或 position 或其他

I assume you have one (in my sample I will use ts column as such)我假设你有一个(在我的示例中,我将使用 ts 列)

在此处输入图像描述

Consider below approach to get what you want考虑下面的方法来获得你想要的

select * from your_table
qualify (lag(page_type) over win = 'plp' and page_type = 'pdp')
or (lead(page_type) over win = 'pdp' and page_type = 'plp')
window win as (partition by sessionId order by ts)

with output与 output

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM