Oracle根据另一个表中的条件从表中选择随机行(分层抽样)

时间:2015-07-28 04:08:29

标签: sql oracle random-sample

我有2张桌子。第一个是基表,其中包含信息,第二个表具有我需要根据表2中的条件从表1中选择的随机行的标准。由于表中条件的排列接近,因此不希望手动执行此操作。到百。需要代码来尝试从我的数据库中获取随机分层样本。

TABLE_1

   ID          Fruit       Subject 
   123       Apple      Math
   124       Apple      Science  
   125       Apple      History  
   126       Apple      Math  
   127       Apple      Science  
   128       Orange     Math  
   129       Orange     Science  
   130       Orange     Science  
   131       Orange     Science  
   132       Orange     History  
   133       Orange     Science  
   134       Pineapple  History  
   135       Pineapple  History  
   136       Pineapple  History  
   137       Pineapple  History  
   138       Pineapple  Math  

表_2

   Fruit      Subject   Count
   Apple        Math        1
   Apple        Science     2
   Apple        History     1
   Orange       Science     2
   Orange       History     1
   Pineapple    History     3
   Pineapple    Math        1  

输出(基于表2中的标准的随机选择)

   ID         Fruit       Subject 
   123      Apple       Math
   124      Apple       Science
   125      Apple       History
   127      Apple       Science
   128      Orange      Math
   129      Orange      Science
   130      Orange      Science
   132      Orange      History
   134      Pineapple   History
   135      Pineapple   History
   137      Pineapple   History
   138      Pineapple   Math

1 个答案:

答案 0 :(得分:1)

听起来您可以使用dbms_random对行进行排序,并使用分析函数row_number在分区中对它们进行排序

select id,
       fruit,
       subject
  from(select t1.*,
              t2.cnt,
              row_number() over (partition by t1.fruit, t1.subject
                                     order by dbms_random.value) rnk
         from table_1 t1
              join table_2 t2
                on( t1.fruit   = t2.fruit and
                    t1.subject = t2.subject ))
 where rnk <= cnt