0
表DATA_CUBE的以下示例数据位于varchar(255)中。它有180万行,没有主键。我想在几秒钟内返回一辆自行车在车站花费的平均时间。首先,我运行了一个查询,将结果顺序返回1,4,5。从下面的快照中,我想知道的是,是否可以计算第1行的stoptime
与第2行的starttime
之间的时差(由bikeid使用SQL?如何查找SQL Server中两个交叉列之间的时间(秒)
表DATA_CUBE的以下示例数据位于varchar(255)中。它有180万行,没有主键。我想在几秒钟内返回一辆自行车在车站花费的平均时间。首先,我运行了一个查询,将结果顺序返回1,4,5。从下面的快照中,我想知道的是,是否可以计算第1行的stoptime
与第2行的starttime
之间的时差(由bikeid使用SQL?如何查找SQL Server中两个交叉列之间的时间(秒)
你真正想要的功能是lead()
,但SQL Server 2008不支持。下面是使用outer apply
的方法:
select dc.*, datediff(second, dc.stoptime, nextdc.starttime) as SecondsToNext
from data_cube dc outer apply
(select top 1 dc2.*
from data_cube dc2
where dc2.bikeid = dc.bikeid and dc2.starttime >= dc.stoptime
order by dc2.starttime
) nextdc;
感谢@Gordon,但此查询抛出错误8120我需要在此查询的结尾使用组? – 2014-10-19 00:29:18
@ReeyaOberoi。 。 。不,我原本以为你想要平均值,并且意外地离开了这个功能。 – 2014-10-19 00:31:19