sql - How to calculate time difference between current and previous row in MySQL -
i have mysql table t1 :
what want do calculations between rows , save value in new coloumn called diff
ticketid| datenew | diff 16743 12:36:46 0 16744 12:51:25 15. minute 16745 12:57:25 6.5 minute .......... ....... etc
i know there similar questions ,but ive tried of solutions posted here no success,so how solve query ???
to time difference in minutes between current , previous row, can use timestampdiff
on datenow
, previous time, can via subquery:
select ticketid, datenew, timestampdiff(minute,datenew,(select datenew mytable t2 t2.ticketid < t1.ticketid order t2.ticketid desc limit 1)) diff mytable t1
update
here's way using variable store previous datenew
value might faster:
select ticketid, datenew, timestampdiff(minute,datenew,prevdatenew) ( select ticketid, datenew, @prevdatenew prevdatenew, @prevdatenew := datenew mytable order ticketid ) t1
Comments
Post a Comment