暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 基于条件创建随机集

askTom 2017-02-02
135

问题描述

我有一张表格,上面有2000条不同种族的男性和女性的记录。我想用以下标准随机创建10个50人的集合

50名女性(即25名女性)的50% (可以是非白人女性)
50人(即15人)中的30%非白人
50名(即10名)白人男性中的20%


这些列基本上是:个人ID_NUM、性别( M或F )和种族( B=黑人、W=白人、H=西班牙裔)

再次,我想创建10个独立的数据集,包括50名患者,这些数据都是由这些细分数据组成的。有什么好的方法可以使用SQL来做到这一点吗?我好像找不到任何能解决问题的方法。这是一个非常小的数据样本,所以我可以手工做,但我认为这个挑战很有趣。感谢您提供的任何帮助/信息

专家解答

我假设这个标准的意思是“至少”,因为(例如)前两个标准的意思是在一个特定的群体中,你可以有多达80%的女性。

您可以这样做-以随机顺序获得所需的总体样本总数,然后将它们分割成组。我不会说这是特别可伸缩的: -)

SQL> create table t (
  2    id int,
  3    gender varchar2(1),
  4    race  varchar2(1)
  5    );

Table created.

SQL>
SQL> exec dbms_random.seed(0); -- just to make it repeatable

PL/SQL procedure successfully completed.

--
-- 10,000 random patients
--

SQL> insert into t
  2  select
  3    rownum,
  4    case when dbms_random.value > 0.6 then 'M' else 'F' end,
  5    case when dbms_random.value > 0.7 then 'A'
  6         when dbms_random.value > 0.4 then 'B'
  7       else 'C' end
  8  from
  9    dual
 10    connect by level <= 100000;

100000 rows created.

SQL>
SQL>
SQL>
SQL> drop table results purge;

Table dropped.

--
-- get 250 females, 150 non race a, and 100 males race a at random
--

SQL>
SQL> create table results as
  2  with
  3    females as
  4      ( select * from
  5          ( select * from t where gender = 'F' order by dbms_random.value )
  6       where rownum <= 250 ),
  7    race_non_a as
  8      ( select * from
  9        ( select * from t where race != 'A'
 10            and id not in ( select id from females ) order by dbms_random.value )
 11       where rownum <= 150 ),
 12    males_race_a as
 13      ( select * from
 14        ( select * from t where gender = 'M' and race = 'A'
 15            and id not in ( select id from females )
 16            and id not in ( select id from race_non_a ) order by dbms_random.value )
 17       where rownum <= 100 )
 18   select mod(rownum,10) grp, f.*
 19   from females f
 20   union all
 21   select mod(rownum,10) grp, r.*
 22   from race_non_a r
 23   union all
 24   select mod(rownum,10) grp, m.*
 25   from males_race_a m
 26  order by 1, 2;

Table created.

SQL>
SQL> select
  2    grp,
  3    count(case when gender = 'F' then 1 end)/ count(*) females,
  4    count(case when gender = 'M' and race = 'A' then 1 end)/ count(*) males_race_a,
  5    count(case when race != 'A' then 1 end)/ count(*) race_non_a
  6  from results
  7  group by grp
  8  order by 1;

       GRP    FEMALES MALES_RACE_A RACE_NON_A
---------- ---------- ------------ ----------
         0        .68           .2        .64
         1        .68           .2        .64
         2        .74           .2        .62
         3        .68           .2        .62
         4        .66           .2        .64
         5        .68           .2         .6
         6        .68           .2        .66
         7        .64           .2        .66
         8         .7           .2        .68
         9        .64           .2         .7

10 rows selected.

SQL>
SQL>
SQL>



「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论