select [ID]=identity(int,1,1),* into # from 表select t,b,c,tid,cid 
from # A
where not exists(select 1 from # where tid=A.tid and cid=A.cid and ID<A.ID)drop table #

解决方案 »

  1.   

    --生成测试数据
    create table #a(t int,b int,c int,tid int,cid int)
    insert into #a select 1 ,2 ,  23,   2 ,   3
    insert into #a select 33,11,  21,   29,   7
    insert into #a select 33,11,  21,   29,   7
    insert into #a select 1 ,2 ,  23,   29,   7
    insert into #a select 21,55,  87,   1 ,   6
    insert into #a select 12,66,  11,   2 ,   6
    insert into #a select 12,5 ,  8 ,   2 ,   6
    insert into #a select 5 ,7 ,  8 ,   2 ,   6
    insert into #a select 21,55,  87,   2 ,   6 --执行查询
    select 
        t.*
    from 
        #a t
    where
        t.b in (select top 2 b from #a where tid=t.tid and cid=t.cid)
    --输出结果
    1 2 23 2 3
    33 11 21 29 7
    33 11 21 29 7
    21 55 87 1 6
    12 66 11 2 6
    12 5 8 2 6
      

  2.   

    select [ID]=identity(int,1,1),* into #a from a
    select
    a.*
    from
    #a a
    where
    a.b in (select top 2 b from #a where tid=a.tid and cid=a.cid)