A表
A1   A2   A3   A4   A5    
1    AA   11   23   34
2    BB   22   45   67
3    AA   11   45   56
5    BB   22   23   34
6    AA   11   45   67
7    BB   22   45   56要求A2 和A3相同的记录,,并取唯一值,并按倒序...结果:
6    AA   11   45   67
7    BB   22   45   56

解决方案 »

  1.   

    select * from tb as a
       where not exists(select * from tb
                          where a.A2=A2 and a.A3=A3 and A.A1<A1)
      

  2.   

    select * from tb as a
       where not exists(select * from tb b
       where a.A2=b.A2 and b.A3=a.A3 and (b.A4>a.A4 
        or  (b.A4=a.A4 and b.A5>a.A5)))
      

  3.   


    select *
    from tb t
    where a1 = (select max(a1) from tb where a2 = t.a2 and a3 = t.a3)
      

  4.   

    select * from tb as a
       where not exists(select * from tb b
       where a.A2=b.A2 and b.A3=a.A3 and b.A1>a.A1)
      

  5.   

    select distinct top 2 a.* from Table_1 a,Table_1 b
    where a.A2 = b.A2 and a.A3 = b.A3
    order by A1  desc
      

  6.   

    select
     *
    from 
     tb t
    where
     a1 = (select max(a1) from tb where a2 = t.a2 and a3 = t.a3)