我目前正在使用CTA来创建和填充一个带有随机值的表。
我想更改CTA以添加一个新的列work\u days varchar2(7)。对于所有行,这个新列的所有7个字节如何随机填充“y”或“n”?
例子:
年
重写创建和清空表的过程,首先使用2个循环,然后将所有内容连接在一起。
Create table employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
last_name VARCHAR2(20),
card_num VARCHAR2(10),
work_days VARCHAR2(7)
);
insert into employees. (employee_id, first_name, last_name, card_num, work_days)
with loop1(employee_id) as. (select level from
dual connect by level <= 50)
,loop2(b) as (select level from dual connect by level <= 7)
SELECT employee_id,
CASE round(dbms_random.value(1,20))
WHEN 1 THEN 'Albert'
WHEN 2 THEN 'Tom'
WHEN 3 THEN 'Anna'
WHEN 4 THEN 'Ty'
WHEN 5 THEN 'Andy'
WHEN 6 THEN 'Thomas'
WHEN 7 THEN 'Alan'
WHEN 8 THEN 'Tara'
WHEN 9 THEN 'Cheryl'
WHEN 10 THEN 'Ed'
WHEN 11 THEN 'Steve'
WHEN 12 THEN 'Mel'
WHEN 13 THEN 'Micheal'
WHEN 14 THEN 'Ron'
WHEN 15 THEN 'Donald'
WHEN 16 THEN 'Donny'
WHEN 17 THEN 'Racheal'
WHEN 18 THEN 'Debbie'
WHEN 19 THEN 'Madison'
WHEN 20 THEN 'Danny'
END AS first_name,
CASE round(dbms_random.value(1,20))
WHEN 1 THEN 'Andrews'
WHEN 2 THEN 'Thorton'
WHEN 3 THEN 'Smith'
WHEN 4 THEN 'Jones'
WHEN 5 THEN 'Ott'
WHEN 6 THEN 'Stevens'
WHEN 7 THEN 'Feldman'
WHEN 8 THEN 'Stein'
WHEN 9 THEN 'Ross'
WHEN 10 THEN 'Eden'
WHEN 11 THEN 'Saltzman'
WHEN 12 THEN 'Kramer'
WHEN 13 THEN 'Monroe'
WHEN 14 THEN 'Hanks'
WHEN 15 THEN 'Dunn'
WHEN 16 THEN 'Dunbar'
WHEN 17 THEN 'Rucker'
WHEN 18 THEN 'Silverberg'
WHEN 19 THEN 'Daniels'
WHEN 20 THEN 'Kahn'
END AS last_name,
dbms_random.string('X', dbms_random.value(5, 10)) AS card_num,
replace(listagg(CASE round(dbms_random.value(1,2))
WHEN 1 THEN 'Y'
WHEN 2 THEN 'N'
END,',') within group (order by b)
,',') AS work_days
FROM loop1
cross join loop2
group by employee_id;
2条答案
按热度按时间e5njpo681#
我在获取非唯一值时遇到了一些困难,但这头野兽成功了:
D小提琴
想法很简单,下面的数字。5变成“y”,否则变成“n”。然后把这个长串连起来切成50块。
第二,也许更简单的解决办法是
merge
:D小提琴
q5lcpyga2#
您可以使用一个函数将employee\u id转换为二进制值,并将其转换为ys和ns的组合。
函数(此处为原件)
您可以在虚拟列中使用它(参见dbfiddle),也可以调用它并传入雇员id。
查询