一个表,有3列,a, b, c,其中a是主键
数据假设是:
a    b    c
a1   b1   c1
a2   b3   c1
a3   b1   c2
a4   b2   c2
a5   b2   c2
我以c找出前2笔数据,也就是我需要的结果只有:a    b    c
a1   b1   c1
a2   b3   c1
a3   b1   c2
a4   b2   c2只能用SQL语句来实现~
急盼!!!

解决方案 »

  1.   

    select * from table where C in (select c from table where rownum<3 order by c desc)
      

  2.   

    select * from Table
    where row_number()over(partition by c ) <3
      

  3.   

    谢谢shyming先
    但你可能没理解我的意思,比如c1有3笔, c2有5笔,c3有10笔,我只希望得到6笔数据,那就是
    c1的前两笔 + c2前两笔 + c3前两笔
      

  4.   

    select * from (
    select c
    ,row_number()over(partition by c order by A) ss from  (select   'a1' A,   'b1' B,'c1' C from dual union
      select  'a2' A,    'b1' B,'c1' C from dual union
      select   'a3' A,   'b1' B,'c1' C  from dual union
       select 'a4' A,    'b1' B ,'c2' C  from dual union
      select   'a5' A,   'b1' B,'c2' C  from dual union
       select 'a6' A,    'b1' B ,'c2'  C from dual  ) T ) tt
     where ss<3
      

  5.   

    c列会有上千种可能,整个table的资料会在十万数量级呢~~
    而且要能在PL/SQL中运行的语句哦~~
      

  6.   

    不过我这里是对A做了排序,就是说每个C中的两个是A最小的两个,你可以自己改。
      

  7.   

    select * from (select a,b,c,row_number()over(partition by c order by A) ss from table ) tt where ss<3
    子查询是会慢,应该可以试试,不知道谁有更好的方法
      

  8.   


    select a,b,c
     from TABLE_NAME T1    
       where  exists
       (
       select C,COUNT(1) from
    TABLE_NAME T2 
       where t2.c=t1.c 
       and t2.a < t1.a 
       GROUP BY C
       HAVING COUNT(1) <3)