我有一张名为" Tickets"看起来如此:
为了简单起见,这里是一张票的一组数据。
我想总计所有门票在"测试"中的总时间(以小时为单位)。在关闭前的状态。因此,在这个示例中,票证在"测试"中花费了2个小时。州。
我所坚持的是时间戳只是一列,因此如果给定的票证进出" TESTING"在它关闭之前我不止一次,我还没有能够找到一个有用的方法。
答案 0 :(得分:0)
这很复杂,因为您必须在每个“测试”记录后获得下一个时间。我会用相关的子查询做到这一点:
select t.ticket_id,
sum(timestampdiff(hour, t.next_update_time, t.update_time)) as hours
from (select t.*,
(select t2.update_time
from tickets t2
where t2.ticket_id = t.ticket_id and
t2.to_state <> 'Testing' and
t2.update_time > t.update_time
order by t2.update_time
limit 1
) as next_update_time
from tickets t
where t.to_state = 'Testing'
) t
group by t.ticket_id;
答案 1 :(得分:0)
好的答案,谢谢。我做了以下似乎完美的工作。谢谢你的帮助:
SELECT ROW_NUMBER() OVER(ORDER BY UPDATE_TIME) AS Row, TICKET_ID, TO_STATE, UPDATE_TIME
INTO #A
FROM TICKETS
SELECT ROW_NUMBER() OVER(ORDER BY UPDATE_TIME) AS Row, TICKET_ID, TO_STATE, UPDATE_TIME
INTO #B
FROM TICKETS
SELECT ROW_NUMBER() OVER(ORDER BY UPDATE_TIME) AS Row, TICKET_ID, TO_STATE, UPDATE_TIME
INTO #A
FROM TICKETS
SELECT ROW_NUMBER() OVER(ORDER BY UPDATE_TIME) AS Row, TICKET_ID, TO_STATE, UPDATE_TIME
INTO #B
FROM TICKETS
SELECT SUM(DATEDIFF(MI, #A.UPDATE_TIME, #B.UPDATE_TIME)) AS TOTAL_MINUTES
FROM #A
LEFT JOIN #B
ON #A.ROW=#B.ROW -1
WHERE #A.TO_STATE='TESTING'