查找给定事件后发生的第一个事件

发布于 2025-02-13 20:49:27 字数 625 浏览 1 评论 0原文

我正在使用一张表,该表由许多网络会话组成,其中包括各种事件和事件ID:s。为了简化我的问题,假设我有4列是session_id,event_name和event_id,其中事件ID可用于按上升/降序订购事件。我们还假装我们有很多事件,我对event_name的3个事件特别感兴趣:开放,提交和拒绝。假设这三个事件可以按任何顺序发生。

我想做的是,我想添加一个新的专栏,每个会话都说这两个事件“提交”和“衰落”中的两个事件首先是在事件“ Open”之后。我尝试使用First_value分区功能,但尚未成功使用。

因此,对于活动序列的会话:“ open”,...(中间发生的许多不同事件),“提交”,“衰落”,我想返回“提交”, 对于事件序列的会话:开放,... 对于“开放”之后的“提交”和“下降”的事件都没有发生的会议,我想退还null。

您可以将下表与名称“事件”一起编写示例SQL代码: ”在此处输入图像描述”

我希望这个问题及其公式很清楚。非常感谢您!

真挚地, 贝尔坦

I am working with a table consisting of a number of web sessions with various events and event id:s. To simplify my question, let's say that I have 4 columns which are session_id, event_name and event_id, where the event id can be used to order the events in ascending/descending order. Let's also pretend that we have a large number of events and that I am particularly interest in 3 of the events with event_name: open, submit and decline. Assume that these 3 events can occur in any order.

What I would like to do, is that I would like to add a new column that for each session says which, if any, of the two events 'submit' and 'decline' that first follows the event 'open'. I have tried using the FIRST_VALUE partition function but have not made it successfully work yet.

So for a session with event sequence: 'open', ... (a number of different events happening in between), 'submit', 'decline', I would like to return 'submit',
and for a session with event sequence: open, ... (a number of different events happening in between), 'decline', I would like to return 'decline',
and for a sessions for which none of the events 'submit' nor 'decline' happens after 'open', I would like to return null.

You can use the following table with name 'events' for writing example SQL code:enter image description here

I hope the question and its formulation is clear. Thank you very much in advance!

Sincerely,
Bertan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2025-02-20 20:49:27

在下面使用(假设您每次会话只接受或下降!)

select *, if(event_name != 'open', null, ['decline', 'accept'][ordinal(
  sum(case event_name when 'decline' then 1 when 'accept' then 2 end) over win
  )]) staus
from your_table
window win as (
  partition by session_id order by event_id 
  rows between 1 following and unbounded following
)    

,如果适用于问题中的示例数据 - 输出为

”在此处输入图像描述”

Use below (assuming you have only one accept or decline per session!)

select *, if(event_name != 'open', null, ['decline', 'accept'][ordinal(
  sum(case event_name when 'decline' then 1 when 'accept' then 2 end) over win
  )]) staus
from your_table
window win as (
  partition by session_id order by event_id 
  rows between 1 following and unbounded following
)    

if apply to sample data in your question - output is

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文