两个主要字段
serialnum char 10
chukuflag char 1
其中有些连续的数据chukuflag=0,有些chukuflag=1
想统计chukuflag=0的数据块数,并知道每个块的第一条的serialnum和这个块共有多少条记录
谢谢

解决方案 »

  1.   


    declare @t table (serialnum char(10),chukuflag char(1))insert into @t
    select '0000000001',0 union
    select '0000000002',0 union
    select '0000000003',1 union
    select '0000000004',1 union
    select '0000000005',0 union
    select '0000000006',0 union
    select '0000000007',0 union
    select '0000000008',1 union
    select '0000000009',0 union
    select '0000000010',0select d.serialnum,c.[count]
    from (select min(serialnum) as serialnum,count(*) as [count]
             from (select *,(select max(serialnum) from @t where chukuflag<>a.chukuflag and serialnum<a.serialnum) as gp
                      from @t a
                     ) b
                where chukuflag=0
                group by gp
             ) c
    left join @t d on c.serialnum=d.serialnum/*
    serialnum  count
    ---------- -----------
    0000000001 2
    0000000005 3
    0000000009 2(3 行受影响)
    */
      

  2.   

    CREATE TABLE tb(col1 varchar(10),col2 int)
    INSERT tb SELECT 'a',2
    UNION ALL SELECT 'a',3
    UNION ALL SELECT 'a',6
    UNION ALL SELECT 'a',7
    UNION ALL SELECT 'a',8
    UNION ALL SELECT 'b',3
    UNION ALL SELECT 'b',5
    UNION ALL SELECT 'b',6
    UNION ALL SELECT 'b',7SELECT col1,start_col2=col2,
    end_col2=(
    SELECT MIN(col2) FROM tb aa
    WHERE col1=a.col1 AND col2>=a.col2 
    AND NOT EXISTS(
    SELECT * FROM tb WHERE col1=aa.col1 AND col2=aa.col2+1)),count=end_col2-start_col2
    FROM tb a
    WHERE NOT EXISTS(
    SELECT * FROM tb WHERE col1=a.col1 and col2=a.col2-1)
      

  3.   


    CREATE TABLE tb(col1 varchar(10),col2 int)
    INSERT tb SELECT 'a',2
    UNION ALL SELECT 'a',3
    UNION ALL SELECT 'a',6
    UNION ALL SELECT 'a',7
    UNION ALL SELECT 'a',8
    UNION ALL SELECT 'b',3
    UNION ALL SELECT 'b',5
    UNION ALL SELECT 'b',6
    UNION ALL SELECT 'b',7SELECT col1,

    SELECT MIN(col2) FROM tb aa 
    WHERE col1=a.col1 AND col2>=a.col2 
    AND NOT EXISTS( 
    SELECT * FROM tb WHERE col1=aa.col1 AND col2=aa.col2+1))-col2+1
    FROM tb a 
    WHERE NOT EXISTS( 
    SELECT * FROM tb WHERE col1=a.col1 and col2=a.col2-1)结果:a 2
    a 3
    b 1
    b 3
      

  4.   

    --用游标,或用临时表吧,只有一条的去了
    CREATE table TTT(serialnum char(10),chukuflag char(1))insert into TTT select 'B',0
    insert into TTT select 'A',0
    insert into TTT select 'C',1
    insert into TTT select 'F',1
    insert into TTT select 'D',0
    insert into TTT select 'H',0
    insert into TTT select 'G',0
    insert into TTT select 'E',1
    insert into TTT select 'F',0
    insert into TTT select 'W',0 
    ---------------------------------利用临时表处理
    SELECT *,id=identity(int,1,1),0 a,0 b into #t FROM TTT --drop table #t
    -------------------------------------------------------------
    declare @a int,@b int,@flag int
    select @a=0,@b=0,@flag=0
    update #t set @a=(case when chukuflag=0 then @a+1 else 0 end),a=@a,
    @b=(case when chukuflag=@flag then @b else @b+1 end),@flag=chukuflag,b=@b
    --------------------------
    select serialnum,连续记录数 from #t A right join
    ( select min(id)id,count(*) 连续记录数
      from #t a where chukuflag=0 group by a.b having count(*)>1)B on a.id=b.id
    -------------
    drop table #t
    ------------------
    B          2
    D          3
    F          2