我有一些代码正在为每个父对象生成父对象和随机数目的子对象记录。我希望每个家长有5个或更多的子记录,少于20个。
我运行了几次,我似乎没有或很少得到儿童记录的范围在5-13。
有人能解释一下我怎样才能得到一个更具分布性的子记录值吗。
如果运行下面的最后一个查询,您将看到值6-15中没有count(*)或很少。
毫无疑问,我的逻辑有问题,但我似乎找不到。我也对任何新代码持开放态度,这些新代码可以完成相同的任务,并使用insert all语句生成分布数量的子记录。
我的目标是生成大量的测试数据来检查应用程序查询。现在我只创造了30天的价值。
CREATE TABLE emp_info
(
empid INTEGER,
empname VARCHAR2(50)
);
CREATE TABLE emp_attendance
(empid INTEGER,
start_date DATE,
end_date DATE
);
insert all
-- when rn=1 insert the parent record.
-- 1 will always =1 always insert a
-- child record.
when rn = 1 then into emp_info (empid, empname) values (id, name)
when 1 = 1 then into emp_attendance (empid, start_date, end_date)
values (id, d1, d1 + dbms_random.value (0, .75))
select * from
(
-- get the highest empid as start
-- so this can be run more than once.
-- if never run before start with 0.
with t as (select nvl(max(empid), 0) maxid from emp_info)
select ceil(maxid + level/20) id,
case mod(maxid + level, 20) when 1 then 1 end rn,
-- create an alpha name from 3-15
-- characters in length.
dbms_random.string('U', dbms_random.value(3, 15)) name,
-- set the start date any where from
-- today + 30 days
trunc(sysdate) + dbms_random.value (1, 30) d1,
case when row_number() over (partition by ceil(maxid + level/20) order by level) > 5 then
-- Ensure there is a minimum of
-- 5 child and a max of 20 records
-- for each parent.
--
-- Exclude first 5 records and then
-- for 6-20 records, generating
-- random number between 5-20.
-- We can then compare with any
-- number between 5-20 so that it
-- can give us any number of
-- records.
dbms_random.value(5, 20) else 5 end as random_val from t
connect by level <= 20 * 1000
)
where random_val <= 19;
-- why is this where clause neeed?
SELECT empid, count(*)
from emp_attendance
group by empid order by empid
EMPID COUNT(*)
1 20
2 20
3 20
4 18
5 19
6 20
7 20
8 19
9 20
10 20
11 19
……
50. 20
2条答案
按热度按时间iibxawm41#
像这样的事情应该能让你走。
ukdjmx9f2#