如何计算SQL中间隔的平均时间?
我有一个 SQL 表,我需要计算出每个事务后的平均时间:
数据如下:
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
该表是事务表,有三列:-
ID = UniqueID、TimeStamp = DataTime、CustomerId = UniqueID
因此,如果我传入参数 DateTime.. 假设“10/09/2011” 我试图实现的结果是..
日期:10/9/2011 AverageQueueTime:3mins2secs - 例如
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用这样的查询(未经测试)
将在几秒钟内给你结果。看看 这个答案以获得更好的解释
With a query like this (not tested)
Will give you the result in seconds. Look at this answer for a better explanation
假设您的表被命名为“MyTable”(它真的被命名为Transaction吗!?)并且您希望在几分钟内得到差异:
这是一个相关子查询。
Assuming your table is named "MyTable" (Is it really named Transaction!?) and that you want the difference in minutes:
This is a correlated subquery.
的 iridio 解决方案的修改版本
a) 查询 SQL Server 数据库版本
b) 支持空值
注意:数据应按时间戳排序,否则会得到错误的结果。
Modified version of the iridio solution for
a) Query for SQL Server Database version
b) Support for null values
Note: The data should be sorted by timestamp else you would get wrong results.