我有上表,我需要做的是,例如根据此图片,返回一行会显示:
Ticket ID | Created | Assigned | Time Difference
2 | 2015-05-31 09:18:44 | 2015-06-03 10:05:00 | $MATH_RESULT
此图片仅显示了一个示例,但我希望让它返回同时分配了''时间和创造的'时间。
我不是DBA,这有点超出了我的有限知识。有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
您可以使用join
查找状态为“已创建”的行的“已分配”行:
select c.ticket_id
, c.timestamp
, a.timestamp
, datediff(a.timestamp, c.timestamp)
from ticket c
join ticket a
on c.ticket_id = a.ticket_id
where c.state = 'created'
and a.state = 'assigned'
答案 1 :(得分:1)
http://sqlfiddle.com/#!9/cf498/3
SELECT t.ticket_id,t.timestamp as created, t1.timestamp as assigned, (t1.timestamp - t.timestamp) as delay
FROM table1 t
LEFT JOIN table1 t1
ON t.ticket_id = t1.ticket_id
AND t1.state = 'assigned'
WHERE t.ticket_id = 2
and t.state = 'created'
或者如果您需要所有门票列表
SELECT t.ticket_id,t.timestamp as created, t1.timestamp as assigned, (t1.timestamp - t.timestamp) as delay
FROM table1 t
LEFT JOIN table1 t1
ON t.ticket_id = t1.ticket_id
AND t1.state = 'assigned'
WHERE t.state = 'created'