gpt4 book ai didi

postgresql - postgres 中的 GROUP BY 列和子句

转载 作者:行者123 更新时间:2023-11-29 12:10:45 25 4
gpt4 key购买 nike

我想按列值以及满足另一个条件时对表的列进行分组。例如,下表:

事件:

id  session_id  flags        created_at  ...
--------------------------------------------
1 100 OTHER ...
2 101 OTHER ...
3 101 NEW_SESSION ...
4 101 OTHER ...
5 101 NEW_SESSION ...
6 100 OTHER ...
7 102 OTHER ...

我想要以下结果:

session_id  events_count first_event_id  last_event_id  
-------------------------------------------------------
100-0 2 1 6
101-0 1 2 2
101-1 2 3 4
101-2 1 5 5
102-0 1 7 7

基本思想是我想从事件中提取 session 。它们按 session_id 分组。每当我有标志 NEW_SESSION 时,我也想要一个新 session 。

查询是这样的:

SELECT ? as session_id
, count(id) as events_count
, MIN(id) as first_event_id
, MAX(id) last_event_id
GROUP BY session_id
-- , and whenever flags is NEW_SESSION
ORDER BY id

但我不知道如何按条件正确表达组。有什么想法吗?

最佳答案

更新 2

在评论中,我注意到您希望它们独一无二。然后我们可以使用一个变量:

SET @inc := 0;

(
SELECT CONCAT(session_id, '-', !ABS(STRCMP(flags, 'NEW_SESSION'))) AS session_id
, COUNT(id) AS events_count
, MIN(id) AS first_event_id
, MAX(id) last_event_id
FROM events
WHERE flags != 'NEW_SESSION'
GROUP BY events.session_id, events.flags
ORDER BY events.id
) UNION (
SELECT CONCAT(session_id, '-', @inc := @inc + 1) AS session_id
, COUNT(id) AS events_count
, MIN(id) AS first_event_id
, MAX(id) last_event_id
FROM events
WHERE flags = 'NEW_SESSION'
GROUP by events.id
ORDER BY events.id
);

更新

以下阻止对 NEW_SESSION 行进行分组:

(
SELECT CONCAT(session_id, '-', !ABS(STRCMP(flags, 'NEW_SESSION'))) AS session_id
, COUNT(id) AS events_count
, MIN(id) AS first_event_id
, MAX(id) last_event_id
FROM events
WHERE flags != 'NEW_SESSION'
GROUP BY events.session_id, events.flags
ORDER BY events.id
) UNION (
SELECT CONCAT(session_id, '-1') AS session_id
, COUNT(id) AS events_count
, MIN(id) AS first_event_id
, MAX(id) last_event_id
FROM events
WHERE flags = 'NEW_SESSION'
GROUP BY id
ORDER BY events.id
);

原始答案

据我了解,您正在尝试按 session ID 和“是否是 NEW_SESSION”标志。如果是这样,那么我会这样表达:

SELECT CONCAT(session_id, '-', !ABS(STRCMP(flags, 'NEW_SESSION'))) AS session_id
, COUNT(id) AS events_count
, MIN(id) AS first_event_id
, MAX(id) last_event_id
FROM events
GROUP BY events.session_id, events.flags
ORDER BY events.id;

关于postgresql - postgres 中的 GROUP BY 列和子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36766317/

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