【问题标题】:Group similar consecutive events together in PostgreSQL在 PostgreSQL 中将相似的连续事件分组在一起
【发布时间】:2021-04-16 14:27:36
【问题描述】:

我是 SQL 新手。我目前正在学习使用按时间排序的用户事件的数据集。例如,示例子集如下 -

EventID UserID EventName Timestamp
1 1 search_event 2020-01-20 09:42:52
2 1 search_event 2020-01-20 09:42:58
3 2 search_event 2020-01-20 09:43:27
4 1 checkout_event 2020-01-20 09:43:49
5 2 checkout_event 2020-01-20 09:43:54
6 2 search_event 2020-01-20 09:44:12
7 1 search_event 2020-01-20 09:54:21
8 1 search_event 2020-01-20 12:45:10
9 1 search_event 2020-01-20 12:45:32
10 2 booking_event 2020-01-20 12:46:52

我想统计自第一个事件发生后每隔 10 分钟发生的不同事件的总数。

  1. 用户 1 在 09:42:52 进行了 search_event。在接下来的 10 分钟内,即直到 09:52:52,用户 1 的每个搜索事件都不会被计算在内。(即在计数中忽略 09:42:58 的搜索事件)
  2. 用户 2 在 09:43:27 进行了 search_event。因此,直到 9:53:27,用户 2 的任何 search_events 都不会被计算在内。
  3. 用户 1 在 09:43:49 进行了 checkout_event。省略用户 1 至 09:53:49 之前所有结帐事件的计数
  4. 用户 2 在 09:43:54 进行了 checkout_event。忽略用户 2 到 09:53:54 之前的所有结帐事件的计数

基本上会统计以下事件——(由 event_ids 通知)

Search - 1,3,7,8
Checkout - 4,5
Booking - 10

作为计数输出 -

Search 4
Checkout 2
Booking 1

由于时间重叠,以下事件被省略。

Event ID 2 - Occurs within 10mins of Event ID 1
Event ID 6 - Occurs within 10mins of Event ID 3
Event ID 9 - Occurs within 10mins of Event ID 8

谢谢

【问题讨论】:

  • 如果您在一小时内有一系列相隔 1 或 5 分钟的事件怎么办?都删除了吗?此外,仅使用您真正使用的数据库进行标记。
  • @GordonLinoff 不,这些事件不会被删除。基本上我认为 10 分钟的时间是可配置的。所以我可以缩短或延长 10 分钟,而不是 10 分钟。但在 10 分钟(配置时间)后,它会再次计算事件,然后在接下来的 10 分钟内不计算。希望我说得通。我以前没有提供这种清晰度是不好的。还进行编辑以仅保留 Postgres 作为标签。谢谢!

标签: sql postgresql


【解决方案1】:

您可以使用简单的not exists检查前10分钟内是否存在相同记录:

with a(EventID, UserID, EventName, ts) as (
  select 1, 1, 'search_event', timestamp '2020-01-20 09:42:52' union all
  select 2, 1, 'search_event', timestamp '2020-01-20 09:42:58' union all
  select 3, 2, 'search_event', timestamp '2020-01-20 09:43:27' union all
  select 4, 1, 'checkout_event', timestamp '2020-01-20 09:43:49' union all
  select 5, 2, 'checkout_event', timestamp '2020-01-20 09:43:54' union all
  select 6, 2, 'search_event', timestamp '2020-01-20 09:44:12' union all
  select 7, 1, 'search_event', timestamp '2020-01-20 09:54:21' union all
  select 8, 1, 'search_event', timestamp '2020-01-20 12:45:10' union all
  select 9, 1, 'search_event', timestamp '2020-01-20 12:45:32' union all
  select 10, 2, 'booking_event', timestamp '2020-01-20 12:46:52'
)
select *
from a
where not exists (
  select null
  from a as f
  where f.userid = a.userid
    and f.eventname = a.eventname
    and f.ts > a.ts - interval '10' minute
    and f.ts < a.ts 
)
order by eventid
事件ID |用户名 |事件名称 | ts ------: | -----: | :------------- | :----------------- 1 | 1 |搜索事件 | 2020-01-20 09:42:52 3 | 2 |搜索事件 | 2020-01-20 09:43:27 4 | 1 |结帐事件 | 2020-01-20 09:43:49 5 | 2 |结帐事件 | 2020-01-20 09:43:54 7 | 1 |搜索事件 | 2020-01-20 09:54:21 8 | 1 |搜索事件 | 2020-01-20 12:45:10 10 | 2 |预订活动 | 2020-01-20 12:46:52

db小提琴here

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 2023-03-11
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    相关资源
    最近更新 更多