--
create table tb(编号 varchar(10),序号 int,数量 int)
insert into tb values('001',1,1)       
insert into tb values('001',2,2)       
insert into tb values('001',3,3)       
insert into tb values('001',4,4)       
insert into tb values('001',5,5)   
insert into tb values('002',1,1)       
insert into tb values('002',2,2)       
insert into tb values('002',3,3)       
insert into tb values('002',4,4)       
insert into tb values('002',5,5)
goselect a.* from tb a where 2 > (select count(*) from tb where 编号 = a.编号 and 序号 < a.序号 ) order by a.编号,a.序号drop table tb/*
编号         序号          数量
---------- ----------- -----------
001        1           1
001        2           2
002        1           1
002        2           2(4 行受影响)
*/

解决方案 »

  1.   

    create table tb(
    id char(3),
    a int,
    b int)insert tb select  '001',1,1
    insert tb select  '001',2,2   
    insert tb select  '001',3,3   
    insert tb select  '001',4,4   
    insert tb select  '001',5,5 
    insert tb select  '002',1,1   
    insert tb select  '002',2,2   
    insert tb select  '002',4,4   
    insert tb select  '002',5,5 select a.id,a.a,a.b
    from tb a,tb b
    where a.id=b.id
    group by a.id,a.a,a.b
    having count(case when a.a>=b.a and a.b>=b.b then 1 else null end)<=2drop table tb/*
    id   a           b           
    ---- ----------- ----------- 
    001  1           1
    001  2           2
    002  1           1
    002  2           2(所影响的行数为 4 行)
    */
      

  2.   


    select id=identity(int,1,1),* into # from tselect * from # a
    where (select count(1) from # where a.编号=编号 and id<=a.id)<2
      

  3.   


    create table tb(编号 varchar(10),序号 int,数量 int)
    insert into tb values('001',1,1)       
    insert into tb values('001',2,2)       
    insert into tb values('001',3,3)       
    insert into tb values('001',4,4)       
    insert into tb values('001',5,5)   
    insert into tb values('002',1,1)       
    insert into tb values('002',2,2)       
    insert into tb values('002',3,3)       
    insert into tb values('002',4,4)       
    insert into tb values('002',5,5)
    goselect id=identity(int,1,1),* into # from tbselect  编号 ,序号 , 数量   
    from # a
    where (select count(1) from # where a.编号=编号 and id<a.id)<2/*
    编号         序号          数量          
    ---------- ----------- ----------- 
    001        1           1
    001        2           2
    002        1           1
    002        2           2(4 row(s) affected)
    */
      

  4.   

    --2
    create table tb(编号 varchar(10),序号 int,数量 int)
    insert into tb values('001',1,1)       
    insert into tb values('001',2,2)       
    insert into tb values('001',3,3)       
    insert into tb values('001',4,4)       
    insert into tb values('001',5,5)   
    insert into tb values('002',1,1)       
    insert into tb values('002',2,2)       
    insert into tb values('002',3,3)       
    insert into tb values('002',4,4)       
    insert into tb values('002',5,5)
    goselect m.* from tb m where not exists(select 1 from 
    (
      select a.* from tb a where 2 > (select count(*) from tb where 编号 = a.编号 and 序号 < a.序号 )
    ) n
    where n.编号 = m.编号 and n.序号 = m.序号)
    order by m.编号 , m.序号
    drop table tb/*
    编号         序号          数量
    ---------- ----------- -----------
    001        3           3
    001        4           4
    001        5           5
    002        3           3
    002        4           4
    002        5           5(6 行受影响)
    */
      

  5.   

    --1
    create table tb(编号 varchar(10),序号 int,数量 int)
    insert into tb values('001',1,1)       
    insert into tb values('001',2,2)       
    insert into tb values('001',3,3)       
    insert into tb values('001',4,4)       
    insert into tb values('001',5,5)   
    insert into tb values('002',1,1)       
    insert into tb values('002',2,2)       
    insert into tb values('002',3,3)       
    insert into tb values('002',4,4)       
    insert into tb values('002',5,5)
    goselect a.* from tb a where 2 > (select count(*) from tb where 编号 = a.编号 and 序号 < a.序号 ) order by a.编号,a.序号drop table tb/*
    编号         序号          数量
    ---------- ----------- -----------
    001        1           1
    001        2           2
    002        1           1
    002        2           2(4 行受影响)
    */
    --2
    create table tb(编号 varchar(10),序号 int,数量 int)
    insert into tb values('001',1,1)       
    insert into tb values('001',2,2)       
    insert into tb values('001',3,3)       
    insert into tb values('001',4,4)       
    insert into tb values('001',5,5)   
    insert into tb values('002',1,1)       
    insert into tb values('002',2,2)       
    insert into tb values('002',3,3)       
    insert into tb values('002',4,4)       
    insert into tb values('002',5,5)
    goselect m.* from tb m where not exists(select 1 from 
    (
      select a.* from tb a where 2 > (select count(*) from tb where 编号 = a.编号 and 序号 < a.序号 )
    ) n
    where n.编号 = m.编号 and n.序号 = m.序号)
    order by m.编号 , m.序号
    drop table tb/*
    编号         序号          数量
    ---------- ----------- -----------
    001        3           3
    001        4           4
    001        5           5
    002        3           3
    002        4           4
    002        5           5(6 行受影响)
    */