Oracle select 来自 table 的随机行基于另一个 table 中的标准(分层抽样)
Oracle select random rows from table based on criteria in another table (Stratified Sampling)
我有 2 个 table。第一个是基础 table,里面有信息,第二个 table 有标准,我需要根据 Table 2 中的标准从 Table 1 中选择随机行。不想手动执行此操作,因为我的 table 中的标准排列接近百。需要代码来尝试从我的数据库中获取随机分层样本。
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
Table_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
输出(根据Table2中的条件随机选择)
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
听起来您可以使用 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
我有 2 个 table。第一个是基础 table,里面有信息,第二个 table 有标准,我需要根据 Table 2 中的标准从 Table 1 中选择随机行。不想手动执行此操作,因为我的 table 中的标准排列接近百。需要代码来尝试从我的数据库中获取随机分层样本。
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
Table_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
输出(根据Table2中的条件随机选择)
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
听起来您可以使用 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