Retrieving rows randomly in pl/sql query

时间:2016-07-11 22:58:25

标签: sql oracle plsql

I have a table (t1). I know how to retrieve percentage of set randomly.

What I want is to insert 30% of randomly selected rows into t2, and insert remaining 70% into table t3.

Is there any other way except inserting 30% into table t2 and than compare t2 with t1 and insert into t3? This method is not good for me since table is huge.

ps. oracle version - 11g

2 个答案:

答案 0 :(得分:3)

查看ora_hash。使用表的PK(或一些类似的列组合)使用桶9生成哈希,使用0-6的哈希在一个表中生成,而使用7,8或9的那些在另一个表中生成哈希。

答案 1 :(得分:2)

插入所有工作吗?这是我在人力资源员工表中所做的一个,所以我随机订购了30%。那些人得到了一个指标。我在整个表格上做了一个联合,并给它一个0的指标。我拿指标的最大值然后插入所有。如果指标是1进入第一个表,否则剩下的70%进入第二个表。

INSERT ALL
  WHEN (table_one_ind = 1) THEN
INTO table_one
  (
    employee_id,
    first_name,
    last_name,
    email,
    hire_date,
    job_id
  )
  VALUES
  (
    employee_id,
    first_name,
    last_name,
    email,
    hire_date,
    job_id
  )
  ELSE
INTO table_two
  (
    employee_id,
    first_name,
    last_name,
    email,
    hire_date,
    job_id
  )
  VALUES
  (
    employee_id,
    first_name,
    last_name,
    email,
    hire_date,
    job_id
  )
SELECT MAX (table_one_ind) table_one_ind,
  employee_id,
  first_name,
  last_name,
  email,
  hire_date,
  job_id
FROM
  (SELECT t.*,
    1 AS table_one_ind
  FROM
    ( SELECT * FROM employees ORDER BY dbms_random.value
    ) t
  WHERE rownum <=
    ( SELECT ceil(COUNT(*)*.3) FROM employees
    )
  UNION ALL
  SELECT t.*, 0 FROM employees t
  )
GROUP BY employee_id,
  first_name,
  last_name,
  email,
  hire_date,
  job_id