我有一位用户完成了一系列事件。我想捕捉他们完成每个事件的次数和顺序。
因此对于下表 user_events :
name eventname time
Ted a 12:01
Ted b 12:02
Ted b 12:03
Ted b 12:04
Ted c 12:05
Ted b 12:06
Ted b 12:07
Ted c 12:08
Ted b 12:09
Ted b 12:11
Ted b 12:12
我应该得到:
name eventname event_sequence_number time_started frequency
Ted a 1 12:01 1
Ted b 2 12:02 3
Ted c 3 12:05 1
Ted b 4 12:06 2
Ted c 5 12:08 1
Ted b 6 12:09 3
我一直在尝试使用 rank(),dense_rank(),row_number()和 lag(),但不能将它们放在一起。有什么想法吗?
答案 0 :(得分:1)
试试这个。它使用Tabibitosan方法(分组序列范围):Toolbox
PostgreSQL 9.6架构设置:
CREATE TABLE user_events
(user_name varchar(3), eventname varchar(1), event_time time)
;
INSERT INTO user_events
(user_name, eventname, event_time)
VALUES
('Ted', 'a', '12:01'),
('Ted', 'b', '12:02'),
('Ted', 'b', '12:03'),
('Ted', 'b', '12:04'),
('Ted', 'c', '12:05'),
('Ted', 'b', '12:06'),
('Ted', 'b', '12:07'),
('Ted', 'c', '12:08'),
('Ted', 'b', '12:09'),
('Ted', 'b', '12:11'),
('Ted', 'b', '12:12')
;
查询1 :
SELECT t.user_name
,t.eventname
,row_number() OVER (
ORDER BY MIN(event_time)
) AS event_sequence_number
,MIN(event_time) AS time_started
,COUNT(*) as frequency
FROM (
SELECT user_name
,eventname
,event_time
,row_number() OVER (
ORDER BY event_time
) - row_number() OVER (
PARTITION BY eventname ORDER BY event_time
,eventname
) seq
FROM user_events
) t
GROUP BY user_name
,eventname
,seq
ORDER BY time_started
<强> Results 强>:
| user_name | eventname | event_sequence_number | time_started | frequency |
|-----------|-----------|-----------------------|--------------|-----------|
| Ted | a | 1 | 12:01:00 | 1 |
| Ted | b | 2 | 12:02:00 | 3 |
| Ted | c | 3 | 12:05:00 | 1 |
| Ted | b | 4 | 12:06:00 | 2 |
| Ted | c | 5 | 12:08:00 | 1 |
| Ted | b | 6 | 12:09:00 | 3 |