gpt4 book ai didi

sql - Postgres - 按 session 聚合用户事件

转载 作者:行者123 更新时间:2023-11-29 12:15:48 24 4
gpt4 key购买 nike

我有一个表,其中包含如下所示的事件:ID、USER_ID、CREATED_AT、EVENT_NAME。

我正在尝试获取用户通常在 session 中创建的事件序列。当用户事件与上一个事件相隔超过 5 分钟时,新 session 开始。

我尽可能地创建了一个包含此信息的 View :

enter image description here

按顺序读取表格,每次“TIME_DIFF”大于 5 分钟时,新 session 开始。

我现在如何按 session 聚合事件,以便最终得到类似这样的结果?

enter image description here

按照表格下方的 View 和一些测试数据:

CREATE SCHEMA test;

CREATE TABLE test."TRACKING_EVENTS" (
"ID" serial PRIMARY key,
"USER_ID" text,
"CREATED_AT" TIMESTAMP,
"EVENT_NAME" text
);

CREATE VIEW
test."ORDERED_EVENTS"
AS
SELECT
"ID",
"USER_ID",
"CREATED_AT" AS "EVENT_TIME",
"EVENT_NAME",
CASE WHEN
lag("CREATED_AT", 1) OVER (ORDER BY "USER_ID", "CREATED_AT") < "CREATED_AT"
THEN
lag("CREATED_AT", 1) OVER (ORDER BY "USER_ID", "CREATED_AT")
ELSE
NULL
END AS "PREVIOUS_EVENT_TIME"
FROM
test."TRACKING_EVENTS";

CREATE VIEW
test."ORDERED_EVENTS_WITH_DIFF"
AS
SELECT
"ID",
"USER_ID",
"EVENT_TIME",
"EVENT_NAME",
"PREVIOUS_EVENT_TIME",
"EVENT_TIME" - "PREVIOUS_EVENT_TIME" AS "TIME_DIFF"
FROM
test."ORDERED_EVENTS";

-- Period 1
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (1, 'user1', '2019-1-1 01:00:00'::timestamp, 'EVENT_1');
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (3, 'user1', '2019-1-1 01:00:05'::timestamp, 'EVENT_2');
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (5, 'user1', '2019-1-1 01:00:10'::timestamp, 'EVENT_3');

INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (2, 'user2', '2019-1-1 01:00:01'::timestamp, 'EVENT_1');
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (4, 'user2', '2019-1-1 01:00:06'::timestamp, 'EVENT_2');
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (6, 'user2', '2019-1-1 01:00:11'::timestamp, 'EVENT_3');

-- Period 2
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (7, 'user1', '2019-1-1 01:10:00'::timestamp, 'EVENT_1');
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (9, 'user1', '2019-1-1 01:10:05'::timestamp, 'EVENT_2');
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (11, 'user1', '2019-1-1 01:10:10'::timestamp, 'EVENT_3');

INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (8, 'user2', '2019-1-1 01:10:01'::timestamp, 'EVENT_1');
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (10, 'user2', '2019-1-1 01:10:06'::timestamp, 'EVENT_2');
INSERT INTO test."TRACKING_EVENTS" ("ID", "USER_ID", "CREATED_AT", "EVENT_NAME")
VALUES (12, 'user2', '2019-1-1 01:10:11'::timestamp, 'EVENT_3');

最佳答案

我想这就是你想要的:

select user_id, session,
array_agg(event_name order by created_at)
from (select tt.*,
count(*) filter (where prev_ca < created_at - interval '5 minute') over (partition by user_id order by created_at) as session
from (select tt.*,
lag(created_at) over (partition by user_id order by CREATED_AT) as prev_ca
from test."TRACKING_EVENTS" tt
) tt
) tt
group by user_id, session
order by user_id, session;

请注意,这使用 array_agg() 而不是 string_agg()。您使用的是 Postgres,因此 array_agg() 是将多个值组合在一起的好方法。

关于sql - Postgres - 按 session 聚合用户事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55434819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com