[Q]随机抽取前N条记录的问题 
[A]8i以上版本 
select * from (select * from tablename order by sys_guid()) where rownum < N; 
select * from (select * from tablename order by dbms_random.value) where rownum< N; 
注:dbms_random包需要手工安装,位于$ORACLE_HOME/rdbms/admin/dbmsrand.sql 
dbms_random.value(100,200)可以产生100到200范围的随机数 

解决方案 »

  1.   

    也就是说
        select substr(aa,1,10),count(*) from table
        group by substr(aa,1,10)if count(*)<=10 从table中选取出aa来
    if count(*)>10  从table中随机选取10条aa来
      

  2.   

    select * from (select substr(aa,1,10),count(*)  from tablename order by sys_guid() group by substr(aa,1,10)) where rownum < 10;
      

  3.   

    用这一句没有错。
    select * from (select * from t1 order by sys_guid()) where rownum < 10;
      

  4.   

    我再解释一下我的问题,先根据分组判断 
    select substr(aa,1,10),count(*) from table
    group by substr(aa,1,10)
    如果 count(*)<=10 
    则是从table里面把这些记录取出来
    如果 count(*)>10 
    则是从table里面取substr(aa,1,10)=该分组的随机10条记录明白了吗??
                               
      

  5.   

    譬如:
      aa 如下:
     1111111111a 
     1111111111b
     1111111111c
     1111111111d
     1111111111e
     1111111111f
     1111111111g
     1111111111h
     1111111111i
     1111111111j
     1111111111k
     substr(aa,1,10)=1111111111 有12条记录
    现在我要从这12条记录里面随机取10条记录, 当然还有其他的分组 条数>10条的
      

  6.   

    我试验写了一下 ! 《10地就不用考虑了
    只分组就可以了
    其他的加条件   select * from ( select count(*) c,substr(a,1,1) d from a11 group by substr(a,1,1) ) t2 where t2.c >4;  但是我用我自己的表试验了一下 也没有成功 !!
    自己努力吧你
      

  7.   

    select * from tablename  where rownum <= N order by dbms_random.value
    这样就OK 了
      

  8.   

    也就是说
        select substr(aa,1,10),count(*) from table
        group by substr(aa,1,10)if count(*)<=10 从table中选取出aa来
    if count(*)>10  从table中随机选取10条aa来
    ========================
    if count(*)<=10 从table中选取出aa来:用having就可以
      

  9.   

    也就是说
        select substr(aa,1,10),count(*) from table
        group by substr(aa,1,10)if count(*)<=10 从table中选取出aa来
    if count(*)>10  从table中随机选取10条aa来
    ========================
    if count(*)<=10 从table中选取出aa来:用having就可以
    select substr(aa,1,10),count(*) from table
        group by substr(aa,1,10) having count(*) <=10if count(*)>10  从table中随机选取10条aa来:
    select * from 
    (select substr(aa,1,10),count(*) from table
        group by substr(aa,1,10) having count(*) >10
      order by dbms_random.value) 
    where rownum<11要想把这两个结果用一个sql跑出来,只需要用union就可以了。
      

  10.   

    随机取表中数据討論
    http://www.cnoug.org/viewthread.php?tid=20848