id      n       a
-------------------1 a 1
1 b 2
2 c 3
1 d 4
1 e 5
2 f 6
2 r 7
怎么取出表中的2/r/7与1/e/5也就是order by a desc
然后 id不能重复,
只需要n的值,
只要二行.
一条sql怎么写呢?

解决方案 »

  1.   

    select * from tt a where not exists(select 1 from tt where a.id=id
    and a.a<a)
      

  2.   

    http://blog.csdn.net/acmain_chm/article/details/4126306
    分组取前N记录
    经常看到问题,如何取出每组的前N条记录。方便大家参考于是便把常见的几种解法列出于下。 问题:有表 如下,要求取出各班前两名(允许并列第二) Table1 +----+------+------+-----+ | id |SName |ClsNo |Score| +----+------+------+-----+ |  1 |AAAA  |  C1  | 67  | |  2 |...
      

  3.   

    这个写法是否比
     select * from (select * from tt ORDER BY a) as b GROUP BY id 性能更差?
      

  4.   

    SELECT * from xx where (id,a) in
    (select id,max(a) from xx GROUP by id);