alter table tablename add id int identity(1,1)
go
select datediff(ms,a.time,b.time) as '相减'
from tablename a
inner join tablename b
on a.id=b.id-1
where a.id % 2 =1
and b.id % 2 = 0

解决方案 »

  1.   

    楼上的大哥,厉害.但是: ID设为自增,有些行要删除掉,所以id不一定是有规率的.
    所以条件:
    on a.id=b.id-1
    where a.id % 2 =1
    and b.id % 2 = 0
    就通不过.
      

  2.   

    create table #t(time datetime)
    insert into #t select '2005-06-01 08:30'
    insert into #t select '2005-06-01 12:30'
    insert into #t select '2005-06-01 14:30'
    insert into #t select '2005-06-01 18:30'
    insert into #t select '2005-06-01 20:30'select
        max(c.time)-min(c.time)
    from
        (select
             a.time,num = count(*)
         from
             #t a,
             #t b
         where
             a.time >= b.time
         group by
             a.time) c
    group by
        (c.num+1)/2
    having max(c.time)<>min(c.time)
      

  3.   

    我写在楼上的SQL只有在保证 time  数据不存在重复记录的前提下才成立。
      

  4.   

    其实是这句有点难懂,我对join理解较差:
    (select
             a.time,num = count(*)
         from
             #t a,
             #t b
         where
             a.time >= b.time
         group by
             a.time) c
      

  5.   

    --新建一个带标识的临时表
    select id=identity(int,1,1),a_time into #tmp from tb--查询
    select * from #tmp
    select a_time-(select a_time from #tmp   where id=a.id-1) from #tmp a where id%2=0--删除临时表