我有这样的表 tiket_event
,它通过更改每张票的状态来存储所有事件
project_id | ticket_id | 状态 | gmt_update |
---|---|---|---|
P01 | T01 | 打开 | 1620000488 |
P01 | T01 | IN_PROGRESS | 1620000588 |
P01 | T02 | 打开 | 1620000499 |
P01 | T01 | 完成 | 1620000688 |
P01 | T02 | 关闭 | 1620000599 |
P01 | T01 | 关闭 | 1620000788 |
P02 | T03 | 打开 | 1650000788 |
我想在这里做的是:
IN_PROGRESS
,然后检查下一个事件是否为 DONE
。通常 DONE
会出现在 IN_PROGRESS
之后。在异常情况下,可能会出现其他事件(例如:IN_PROGRESS
-> IN_TEST
-> DONE
,我仍然计算这种情况)我怀疑我们可以使用这种语法(只是我从互联网复制的示例查询)
select prev_word, word, next_word
from (
select id,
lag(word) over (order by id) as prev_word,
word,
lead(word) over (order by id) as next_word
from words
) as t
where word = 'name';
但我不知道如果状态为 DONE
---编辑---
我想要的预期结果
project_id | 计数 |
---|---|
P01 | 1 |
P02 | 0 |
有人可以帮忙吗?
谢谢
答案 0 :(得分:-1)
-- IN_PROGRESS and DONE must be adjacent
WITH
cte AS ( SELECT ticket_id,
status = 'IN_PROGRESS' AND LEAD(status) OVER (PARTITION BY ticket_id ORDER BY gmt_update) = 'DONE' normal
FROM tiket_event )
SELECT ticket_id -- or you need COUNT(ticket_id) ?
FROM cte
WHERE normal
或者(任务不够明确)
-- IN_PROGRESS and DONE must not be adjacent
SELECT DISTINCT ticket_id -- or COUNT(DISTINCT ticket_id) ?
FROM tiket_event t1
JOIN tiket_event t2 USING (ticket_id)
WHERE t1.status = 'IN_PROGRESS'
AND t2.status = 'DONE'
AND t1.gmt_update < t2.gmt_update
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=7a79cd09d95fa7e1f9ec2182e8a50662