0

我有这样的交易表:

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。
有没有办法像这样得到平均值?

4

1 回答 1

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
于 2013-07-20T07:05:37.517 回答