say your table name is "yourtable", try something likeselect identity(int, 1,1) id, je, rq into #temp1 from yourtable y where not exists (select 1 from yourtable where bh = y.bh-1 and je = y.je)select identity(int, 1,1) id, je, rq into #temp2 from yourtable y where not exists (select 1 from yourtable where bh = y.bh+1 and je = y.je)select t1.rq as kssj, t2.rq as jssj, t1.je
from #temp1 t1
inner join #temp2 t2
on t1.id = t2.id

解决方案 »

  1.   

    create table #你的表(bh int,rq datetime,je int)
    insert #你的表 values(1 ,  '2003-01-01',         10)
    insert #你的表 values(2,   '2003-01-02' ,        10)
    insert #你的表 values(3   ,'2003-01-03'  ,        15)
    insert #你的表 values(4  , '2003-01-04'   ,      15)
    insert #你的表 values(5 ,  '2003-01-05'    ,     10)
    insert #你的表 values(6,   '2003-01-06'     ,    10)
    insert #你的表 values(7  , '2003-01-07'      ,    20)
    insert #你的表 values(8 ,  '2003-01-08'       ,   10)
    insert #你的表 values(9,   '2003-01-09'        ,  10)declare @a int,@b int
    set @a=0
    select *,0 flag into #b from #你的表update #b set @a=case when je=@b then @a else @a+1 end,@b=je,flag=@aselect min(rq) kssj,max(rq) jssj,max(je) je from #b group by flag
    go
    drop table #你的表,#b
      

  2.   

    kssj                                                   jssj                                                   je          
    ------------------------------------------------------ ------------------------------------------------------ ----------- 
    2003-01-01 00:00:00.000                                2003-01-02 00:00:00.000                                10
    2003-01-03 00:00:00.000                                2003-01-04 00:00:00.000                                15
    2003-01-05 00:00:00.000                                2003-01-06 00:00:00.000                                10
    2003-01-07 00:00:00.000                                2003-01-07 00:00:00.000                                20
    2003-01-08 00:00:00.000                                2003-01-09 00:00:00.000                                10(所影响的行数为 5 行)
      

  3.   

    select a.rq,b.rq,a.je from tmp a inner join 
    (select rq from tmp where bh/2=cast(bh as float(6,2))/2 ) b
    on a.rq+1=b.rq where bh/2<>cast(bh as float(6,2))/2