Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate average time between intervals in SQL?

I have a SQL table where I need to work out the average time after each transactions:

The data looks like:

Tran1 07/09/2011 09:09:07  - CUSTOMER1 
Tran2 07/09/2011 09:30:46  - CUSTOMER1 
Tran3 07/09/2011 11:27:01  - CUSTOMER2 
Tran4 07/09/2011 11:29:22  - CUSTOMER2 
Tran5 07/09/2011 13:23:48  - CUSTOMER1 
Tran6 08/09/2011 14:21:29  - CUSTOMER3 
Tran7 08/09/2011 14:25:23  - CUSTOMER3 
Tran8 10/09/2011 13:28:57  - CUSTOMER1 
Tran9 10/09/2011 13:30:21  - CUSTOMER1 
Tran10 10/09/2011 13:49:13 - CUSTOMER4

The table is Transaction Table and there are three columns:-

ID = UniqueID, TimeStamp = DataTime, CustomerId = UniqueID

So if I pass in a parameter DateTime.. let say '10/09/2011' The result Im trying to achieve is..

Date:10/9/2011 AverageQueueTime:3mins2secs - for exmaple

like image 519
user929153 Avatar asked Mar 10 '26 16:03

user929153


1 Answers

With a query like this (not tested)

select t.customerID, TIMESTAMPDIFF(SECOND, MIN(t.timestamp), MAX(t.timestamp) ) / (COUNT(DISTINCT(t.timestamp)) -1)  as AverageTime
from Transaction_Table T
group by T.customerID

Will give you the result in seconds. Look at this answer for a better explanation

like image 108
Iridio Avatar answered Mar 12 '26 07:03

Iridio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!