这是一个简单的示例,人口规模为4,样本数量为2:
with population as (
select 1 as val from dual union all
select 2 from dual union all
select 3 from dual union all
select 4 from dual)
select val from (
select val, dbms_random.value(0,10) AS RANDORDER
from population
order by randorder)
where rownum <= 2
(对于我来说,oracle sample()函数与WITH子句不兼容)
但是现在,我想“放大”或乘以示例数据。这样我就可以得到总体数据的150%样本数据(例如人口规模4和样本规模6)
通过SQL查询是否有实现此目的的好方法?
答案 0 :(得分:1)
您可以使用CONNECT BY
:
with population(val, RANDOMORDER) as (
select level, dbms_random.value(0,10) AS RANDORDER
from dual
connect by level <= 6
ORDER BY RANDORDER
)
select val
FROM population
WHERE rownum <= 4;
答案 1 :(得分:0)
该解决方案取决于您是否要从第一个初始集合获取所有行,并从最后一个随机集合获取其他行,然后使用:
with params(size_, sample_) as (select 4, 6 from dual)
select val
from (
select mod(level - 1, size_) + 1 val, sample_,
case when level <= size_ * floor(sample_ / size_) then 0
else dbms_random.value()
end rand
from params
connect by level <= size_ * ceil(sample_ / size_)
order by rand)
where rownum <= sample_
但是,如果您允许出现(1,1,2,2,3,3)之类的结果的可能性,其中某些值可能根本不会出现在输出中(此处4
),请使用以下方法:
with params(size_, sample_) as (select 4, 6 from dual)
select val
from (
select mod(level - 1, size_) + 1 val, sample_, dbms_random.value() rand
from params
connect by level <= size_ * ceil(sample_ / size_)
order by rand)
where rownum <= sample_
它如何工作?我们建立(1、2、3、4)集的次数是除法sample / size
的结果。然后,我们分配随机值。在第一种情况下,我将0
分配给第一个集合,因此可以肯定地将它们输出,最后一个集合则是随机值。在第二种情况下,将随机数分配给所有行。