现在有表T,里面有字段F1,F2,F3
其中字段 F3 有3种类型的值:1,2,3请问,如何查询T表中的F1 = 1,F1 = 2,F1 = 3时,各10条记录。
查询语句如何写?

解决方案 »

  1.   

    select * from t where f1=1 and rownum<=10
    unoin all
    select * from t where f1=2 and rownum<=10
    unoin all
    select * from t where f1=3 and rownum<=10
      

  2.   


    select * from (
    select F1,F2, f3,row_number() over(PARTITION by F3 ORDER by f1 ) as num  from T  
    ) where num<=1
      

  3.   

    select * from (
    select F1,F2, f3,row_number() over(PARTITION by F3 ORDER by f1 ) as num  from T  
    ) where num<=10
      

  4.   

    select * from ( 
    select F1,F2, f3,row_number() over(PARTITION by F3 ORDER by f1 ) as num  from T  
    ) where num <=10 
      

  5.   


    2楼的答案,将unoin 改成union是可以用的
    就是不知道lz的F3里面的数据是否只有1、2、3,如果有上万个数的话,那2楼的方法是不行的select * from ( 
    select F1,F2, f3,row_number() over(PARTITION by F3 ORDER by f1 ) as num  from T  
    ) where num <=10 5楼的答案可以满足比如F3有上万个不同数值的需要,但是row_number() over(PARTITION by F3 ORDER by f1 )函数的方法是,比如F3=1有15行数据,那么就会给每行记录起个标号,从1开始,当F3=2时,标号又会从1开始,问题就是如果F3=1或者F3=2时都有几十万条数据,那么我想速度会很慢期待楼下给出最佳答案
      

  6.   

    unoin 会把相同的合并,但是unionall 相同的不会合并,所以用UNIONALLselect * from t where f1=1 and rownum<=10
    unoin all
    select * from t where f1=2 and rownum<=10
    unoin all
    select * from t where f1=3 and rownum<=10是标准答案!
      

  7.   

    select * from ( 
    select F1,F2, f3,row_number() over(PARTITION by F3 ORDER by f1 ) as num  from T  
    ) where num <=10 
      

  8.   


    对!如果F3字段只有1,2,3三个值的话用row_number()over()是能够查询出来的,不过如果F3字段还有其他
    的取值的话,那么用row_number()over()的方法就不行了。