sql—从两个表中选择随机记录

2g32fytz  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(466)

我有两个源表employees,locations,它们的记录计数不同,还有一个目标表access\u history。
我想随机选取记录(每个表中的一些列)并用这些值填充目标表。
我知道我可以做一个子查询(见下文)来填充access\u history表的employee部分。

INSERT into table access_history
SELECT 
from ( select employee_id, 
                     card_num 
                    from employees 
                    where 
                      last_name like '%' 
        order by dbms_random.value  ) 
      where rownum <= 2

我有两个问题,如何用随机值填充access\u history表的locations部分。
其次,如何用随机但有意义的datetime值填充access\u date列?对于每个随机雇员id的每个记录,有意义的是一个日期层次结构,其中第二个日期比第一个日期大10分钟-5小时,第三个日期比第二个日期大10分钟-5小时,依此类推。
最后,这只是测试数据,我不担心性能问题,因为我知道dbms\u random有开销。我的目标是将其封装在connectbylevel<=10000语句中,以便生成大量的测试数据。
提前感谢所有回答的人。
我的数据应该是这样的:

SELECT * from access_history 
ORDER BY employee_id, access_date 

1, 'AAA1', 101,  '06212020 21:02:04', 0
1, 'AAA1', 102,  '06212020 21:22:54', 0
1, 'AAA1', 104,  06222020 01:13:11', 0
1, 'AAA1', 101,  '06212020 05:29:22', 0

3, 'CCC3', 105,  '06212020 08:42:34',0,
3 'CCC3', 102,  '06212020 16:09:55', 0
3 'CCC3', 104,  '06212020 22:29:53', 0

CREATE TABLE employees
(
   employee_id NUMBER(6),
   first_name VARCHAR2(20),
   last_name VARCHAR2(25) NOT NULL,
   card_num varchar2(10)  NOT NULL
);

ALTER TABLE employees
     ADD ( CONSTRAINT employee_id_pk
   PRIMARY KEY (employee_id));

Insert into employees values (1, 'Mike', 'Jones', 'AAA1');

Insert into employees values (2, 'Jane', 'Doe', 'BBB2');

Insert into employees values (3, 'Paul', 'Smith', 'CCC3');

Insert into employees values (4, 'John', 'Henry', 'DDD4');

 Create table locations(
   location_id NUMBER(4),
   location_name varchar2(30),
   location_type char(1));

 -- A=access T=Time & Attendance 

ALTER TABLE locations 
     ADD ( CONSTRAINT lication_id_pk
   PRIMARY KEY (location_id));

Insert into locations values (101, 'South Front Door 1', 'T');

  Insert into locations values (102, 'South Front Door 2', 'T');

  Insert into locations values (103, 'East Back Door 1', 'T');

   Insert into locations values (104, 'East Back Door 2', 'T');

   Insert into locations values (105,'Computer Room', 'A');

   Insert into locations values (106,'1st Floor North',  'A');

Create table access_history(
   employee_id NUMBER(6), 
   card_num varchar2(10),
   location_id number(4),
   access_date date,
   processed NUMBER(1) default 0
);
qhhrdooz

qhhrdooz1#

可能干净的sql解决方案是可能的,但是我会用两个简单的循环来创建plsql块。它更干净,你可以看到发生了什么,很容易修改:
D小提琴

declare
  v_loc number;
  v_dt date;
begin
  -- take two random employees
  for emp in (select employee_id, card_num 
              from (select employee_id, card_num 
                    from employees order by dbms_random.value ) 
              where rownum <= 2) loop

    -- set starting date
    v_dt := timestamp '2020-06-21 08:00:00';

    -- for each employee generate 3 to 5 rows
    for i in 1..dbms_random.value(3, 5) loop

      -- get random location
      select location_id 
        into v_loc 
        from (select location_id from locations order by dbms_random.value) 
        where rownum = 1;

        -- increase last used date by random 10 to 300 minutes
        v_dt := v_dt + dbms_random.value(10, 300)/(24*60);

        insert into access_history (employee_id, card_num, location_id, access_date)
        values (emp.employee_id, emp.card_num, v_loc, v_dt);

    end loop;
  end loop;
end;

相关问题