求一sql语句
有定义如下的一张表:
create table(ID INT,
DT DATETIME,
V INT
primary key(ID, DT))
如有记录:
1, 2003-1-1 10:00:00, 1
1, 2003-1-1 10:05:00, 2
1, 2003-1-1 11:20:00, 3
2, 2003-1-1 9:00:00, 1
2, 2003-1-2 10:00:00, 2
2, 2003-1-3 11:00:00, 3
表里的数据经过ID和DT的排序后,现在要计算同一个ID相邻两条记录的时间差值,以分钟计算,不知道怎么可以实现呢?
前提是不用游标,因为这张表的数据很大
怎么逐行计算呢?
---定义表结构.,加测试数据
declare @t table( id int,dt smalldatetime, v int)
insert into @t
select 1, '2003-1-1 10:00:00', 1
union all
select 1, '2003-1-1 10:05:00', 2
union all
select 1,' 2003-1-1 11:20:00', 3
union all
select 2, '2003-1-1 9:00:00', 1
union all
select 2, '2003-1-2 10:00:00', 2
union all
select 2, '2003-1-3 11:00:00', 3