表如下
id       序号
a        1
a        2
a        3
b        4
b        5
c        6
要生成以下的查询
id    起号    止号a      1      3
b      4      5
c      6      6

解决方案 »

  1.   

    select id,min(序号) as 起号, max(序号) as 止号 from tablename group by id
      

  2.   

    create table #t(id varchar(10),序号 int)
    insert into #t
    select 'a',1
    union all select 'a',2
    union all select 'a',3
    union all select 'b',4
    union all select 'b',5
    union all select 'c',6
    select id,min(序号) as 起号,max(序号) as 止号
    from #t
    group by id/*
    id         起号          止号          
    ---------- ----------- ----------- 
    a          1           3
    b          4           5
    c          6           6(所影响的行数为 3 行)
    */
      

  3.   

    select * from ta a 
    where not exists(select 1 from ta where id=a.id and 序号>a.序号)