我有这样的交易表:
transactID Locid vtid dtime Paydate
20 5 7 2013-05-07 17:40:42.000 2013-05-07 17:55:42.000
21 5 7 2013-05-07 18:15:17.000 2013-05-07 18:25:17.000
22 5 7 2013-05-07 18:27:44.000 2013-05-07 18:47:44.000
23 5 8 2013-05-08 12:53:54.000 2013-05-08 13:05:24.000
24 5 8 2013-05-08 13:11:21.000 2013-05-08 16:53:03.000
我写了这样的查询来获得SUM
的过时性:
SELECT convert(varchar(10),sum(DATEDIFF(hour,t.Paydate,t.DelDate)))+':' +convert(varchar(10),sum(DATEDIFF(minute,t.Paydate,t.DelDate)% 60)) + ':'
+convert(varchar(10),sum(DATEDIFF(SECOND,t.Paydate,t.DelDate)% 60))
AS ' HH:MM:SS'
FROM Transaction_tbl t
WHERE t.Locid=5
GROUP by vtid
现在我得到输出作为datediffence的总和:
HH:MM:SS
3:44:73
1:8:67
我想把输出作为这个值的平均值。我的意思是第一个答案(3:44:73)是vitd 7的总和,而不是得到SUM
我希望获得总和average
,这里总共3次vtid 7重复。所以答案除以3.
有没有什么方法可以达到这样的平均值?
答案 0 :(得分:0)
试试这个:
select SUM(DATEDIFF(MI,t.Paydate,t.DelDate)) as sum_min,
AVG( CONVERT(NUMERIC(18,0), DATEDIFF(MI,t.Paydate,t.DelDate) ) ) as avg_min
from Transaction_tbl t where t.Locid=5
group by t.vtid