Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从循环输出Oracle SQL创建表_Sql_Oracle_Loops_Plsql - Fatal编程技术网

从循环输出Oracle SQL创建表

从循环输出Oracle SQL创建表,sql,oracle,loops,plsql,Sql,Oracle,Loops,Plsql,我需要从基于175个人口统计选项的约500万个观察数据表中随机抽取一个样本。人口统计表类似于以下表格: 1 40 4% 2 30 3% 3 30 3% - - 174 2 .02% 175 1 .01% 基本上,我需要从500万行表格中随机抽取相同的人口统计数据。对于每个人口统计,我需要从较大的表中选择一个相同的样本,但观察次数为5倍。例如:对于人口统计1,我需要200个随机样本 SELECT * FROM ( SELECT * FROM my_

我需要从基于175个人口统计选项的约500万个观察数据表中随机抽取一个样本。人口统计表类似于以下表格:

1 40 4%
2 30 3%
3 30 3%
- -
174 2 .02%
175 1 .01%
基本上,我需要从500万行表格中随机抽取相同的人口统计数据。对于每个人口统计,我需要从较大的表中选择一个相同的样本,但观察次数为5倍。例如:对于人口统计1,我需要200个随机样本

SELECT  *
FROM    (
        SELECT  *
        FROM    my_table
        ORDER BY
                dbms_random.value
        )
WHERE rownum <= 100;

如果两个表中都存在1-175人口统计值,则可以联接表,或者有一个等效的列可以联接,例如:

select id
from (
  select d.demographic, d.percentage, t.id,
    row_number() over (partition by d.demographic order by dbms_random.value) as rn
  from demographics d
  join my_table t on t.demographic = d.demographic
)
where rn <= 5 * percentage
您只需要ID,但我包含上下文的其他列;或者更简洁地说:

with my_table (id, demographic) as (
  select level, mod(level, 175) + 1 from dual connect by level <= 175000
),
demographics (demographic, percentage, str) as (
            select 1, 40, '4%' from dual
  union all select 2, 30, '3%' from dual
  union all select 3, 30, '3%' from dual
  -- ...
  union all select 174, 2, '.02%' from dual
  union all select 175, 1, '.01%' from dual
)
select demographic, percentage, count(id) as ids, min(id) as min_id, max(id) as max_id
from (
  select d.demographic, d.percentage, t.id,
    row_number() over (partition by d.demographic order by dbms_random.value) as rn
  from demographics d
  join my_table t on t.demographic = d.demographic
)
where rn <= 5 * percentage
group by demographic, percentage
order by demographic;

DEMOGRAPHIC PERCENTAGE        IDS     MIN_ID     MAX_ID
----------- ---------- ---------- ---------- ----------
          1         40        200        175     174825
          2         30        150          1     174126
          3         30        150       2452     174477
        174          2         10      23448     146648
        175          1          5      19074     118649

你们有一张桌子还是两张桌子?我发现很难遵循数据结构。与这些百分比相关的数值是多少?@GordonLinoff我有两个。第一个表包含人口统计、百分比、观察次数数值。该表共有175行。第二张表是我想从中随机抽取的样本,约为500万rows@GordonLinoff我编辑了我的问题,试图使问题更清晰。你不必完全理解,主要的是我不知道从哪里开始循环不同表中的列并聚合结果。请回答你的问题,并为相关表添加CREATETABLE语句。您的示例数据只显示了两列,其中一列是PK?我仍然不清楚循环与通过列的需求循环有什么关系?。为什么要按dbms_random.value排序而不是使用normal子句?一些例子会很有帮助。第一段代码只需替换我的列名/表名就可以很好地工作,感谢您理解我的问题!
-- CTEs for sample data
with my_table (id, demographic) as (
  select level, mod(level, 175) + 1 from dual connect by level <= 175000
),
demographics (demographic, percentage, str) as (
            select 1, 40, '4%' from dual
  union all select 2, 30, '3%' from dual
  union all select 3, 30, '3%' from dual
  -- ...
  union all select 174, 2, '.02%' from dual
  union all select 175, 1, '.01%' from dual
)
-- actual query
select demographic, percentage, id, rn
from (
  select d.demographic, d.percentage, t.id,
    row_number() over (partition by d.demographic order by dbms_random.value) as rn
  from demographics d
  join my_table t on t.demographic = d.demographic
)
where rn <= 5 * percentage;

DEMOGRAPHIC PERCENTAGE         ID         RN
----------- ---------- ---------- ----------
          1         40      94150          1
          1         40      36925          2
          1         40     154000          3
          1         40      82425          4
...
          1         40     154350        199
          1         40     126175        200
          2         30      36051          1
          2         30       1051          2
          2         30     100451          3
          2         30      18026        149
          2         30     151726        150
          3         30     125302          1
          3         30     152252          2
          3         30     114452          3
...
          3         30     104652        149
          3         30      70527        150
        174          2      35698          1
        174          2      67548          2
        174          2     114798          3
...
        174          2      70698          9
        174          2      30973         10
        175          1     139649          1
        175          1     156974          2
        175          1     145774          3
        175          1      97124          4
        175          1      40074          5
with my_table (id, demographic) as (
  select level, mod(level, 175) + 1 from dual connect by level <= 175000
),
demographics (demographic, percentage, str) as (
            select 1, 40, '4%' from dual
  union all select 2, 30, '3%' from dual
  union all select 3, 30, '3%' from dual
  -- ...
  union all select 174, 2, '.02%' from dual
  union all select 175, 1, '.01%' from dual
)
select demographic, percentage, count(id) as ids, min(id) as min_id, max(id) as max_id
from (
  select d.demographic, d.percentage, t.id,
    row_number() over (partition by d.demographic order by dbms_random.value) as rn
  from demographics d
  join my_table t on t.demographic = d.demographic
)
where rn <= 5 * percentage
group by demographic, percentage
order by demographic;

DEMOGRAPHIC PERCENTAGE        IDS     MIN_ID     MAX_ID
----------- ---------- ---------- ---------- ----------
          1         40        200        175     174825
          2         30        150          1     174126
          3         30        150       2452     174477
        174          2         10      23448     146648
        175          1          5      19074     118649