找出日期之间差异的平均值-Sql Select

时间:2013-12-16 12:04:02

标签: sql sql-server select

我有一张下面详细的表格

Repid | buildDate          | BuildVersion
---------------------------------
 1     2013-11-15 10:41:00  1683
 1     2013-11-15 11:10:00  1684
 1     2013-11-15 12:14:00  1685
 2     2013-11-15 10:41:00  1688
 2     2013-11-15 11:10:00  1689
 2     2013-11-15 12:14:00  1690

对于每个Repid,我需要找到连续构建版本之间的平均差异小时数。

1 个答案:

答案 0 :(得分:4)

select  b1.RepId
,       avg(abs(datediff(hour, b1.buildDate, b2.buildDate)))
from    builds b1
join    builds b2
on      b1.BuildVersion = b2.BuildVersion + 1
        and b1.Repid = b2.Repid
group by
        b1.RepId

Live example at SQL Fiddle.