考虑下面的方案
id user tag created
1 foo tag1 2018-09-01
2 foo tag2 2018-09-01
3 bar tag1 2018-09-02
4 bar tag2 2018-09-03
5 kkk tag2 2018-09-05
6 qqq tag1 2018-09-12如何找到唯一用户的数量,在'tag1‘行之后,该用户的未来行后面跟着一个'tag2’?
根据上面的查询,答案是2 (foo,bar)
发布于 2018-09-17 19:01:21
您可以使用聚合来获取用户列表:
select user
from t
group by user
having min(case when tag = 'tag1' then created end) < max(case when tag = 'tag2' then created end);要获取此类用户的数量,请使用子查询:
select count(*)
from (select user
from t
group by user
having min(case when tag = 'tag1' then created end) < max(case when tag = 'tag2' then created end)
) u;发布于 2018-09-17 19:11:55
您可以使用子查询来查找以下行:
SELECT *
FROM yourdata AS t
WHERE tag = 'tag1'
AND EXISTS (
SELECT 1
FROM yourdata AS x
WHERE x.user = t.user
AND x.tag = 'tag2'
AND (x.created > t.created OR x.id > t.id)
)https://stackoverflow.com/questions/52366225
复制相似问题