你可以考虑
查询 1. 使用 ROW_NUMBER 等窗口函数对行进行排序和过滤
查询 2. 使用查找最早开始日期的聚合查询过滤您的记录
请参阅下面的工作小提琴,其中一种方法可以完成上述建议的方法。
查询 #1
SELECT
user,
timestamp,
event
FROM (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY user ORDER BY timestamp) rn
FROM
my_table
) t
WHERE
rn=1;
| user |
timestamp |
event |
| 1 |
2021-02-10 |
answered |
| 2 |
2021-02-11 |
answered |
| 3 |
2021-02-13 |
next question |
| 4 |
2021-02-12 |
next question |
查询 #2
SELECT
t1.user,
t1.timestamp,
t1.event
FROM
my_table t1
INNER JOIN
(
SELECT
user,
MIN(timestamp) as min_timestamp
FROM
my_table
GROUP BY
user
) t2 ON t1.user=t2.user AND
t1.timestamp=t2.min_timestamp;
| user |
timestamp |
event |
| 1 |
2021-02-10 |
answered |
| 2 |
2021-02-11 |
answered |
| 3 |
2021-02-13 |
next question |
| 4 |
2021-02-12 |
next question |
View on DB Fiddle
让我知道这是否适合你。