关注上一个问题: How to remove duplicates so that only pairs exist in a table?
所以我试图为ticket_events表中的每个ticket_uuid过滤出不同的IN / OUTs
例如,如果我有
ticket_uuid timestamp type
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 13:53:13 IN
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 13:59:25 OUT
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 13:59:31 OUT
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 17:15:06 OUT
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 19:35:43 IN
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 19:54:59 OUT
84373463-48c3-3764-74cd-shfhfsfuey12 2018-08-20 19:57:23 OUT
我想要
ticket_uuid timestamp time
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 13:53:13 IN
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 13:59:25 OUT
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 19:35:43 IN
5d6faf4c-c76f-48ed-9780-dbdad1ed06c6 2018-08-20 19:54:59 OUT
84373463-48c3-3764-74cd-shfhfsfuey12 2018-08-20 19:57:23 OUT
但是,如果当前ticket_uuid与前一个票证不同,则允许连续的IN或OUT,如上面的84373463-48c3-3764-74cd-shfhfsfuey12所示。
现在我有:
select ticket_uuid, timestamp, type
from (select t.*,
lag(type) over (order by timestamp) as prev_type,
lag(ticket_uuid) over (order by timestamp) as prev_ticket
from ticket_events t
) t
where prev_type is distinct from type or prev_ticket=ticket_uuid
但是它似乎给了我比我要求更多的结果。我应该如何解决我的查询,使其给出以上内容?谢谢!
答案 0 :(得分:0)
考虑添加一个PARTITION BY
子句(在分组中省略 Type )。然后在外部查询NULL
子句中说明WHERE
:
SELECT ticket_uuid, "timestamp", "type"
FROM (SELECT t.ticket_uuid, t."timestamp", t."type",
LAG("type") OVER (PARTITION BY ticket_uuid ORDER BY "timestamp") as prev_type
FROM ticket_events t
) t
WHERE "type" <> prev_type OR prev_type IS NULL