英文:
Shuffle a column between rows and update
问题
为了测试目的,我有以下表格:
CREATE TABLE DANILO.TESTE2(
CPF VARCHAR2(11),
NOME VARCHAR2(100));
insert into danilo.teste2 values ('93434433443','Sousa');
insert into danilo.teste2 values ('54545454545','Ferreira');
insert into danilo.teste2 values ('99393201832','Cavalcante');
insert into danilo.teste2 values ('13902939392','Silva');
然后,我使用了这个答案来对cpf
列的数据进行洗牌,对于我的情况来说,它运行良好。
select t2.cpf, t.nome
from (select t.*, rownum as seqnum from danilo.teste2 t) t join
(select t.*, row_number() over (order by dbms_random.value) as seqnum from danilo.teste2 t) t2 -- TABELA QUE EMBARALHA
on t.seqnum = t2.seqnum;
但是,我如何对所有的cpf行进行洗牌和更新呢?
我尝试了以下方法:
MERGE INTO danilo.teste2 t1
USING (
SELECT
cpf,
ROW_NUMBER() OVER (ORDER BY DBMS_RANDOM.VALUE) AS random_order
FROM danilo.teste2
) shuffled_data
ON (1 = 1)
WHEN MATCHED THEN
UPDATE SET t1.cpf = shuffled_data.cpf;
但是我得到了ORA-30926: unable to get a stable set of rows in the source tables
错误。
我需要的是将cpf
列的数据进行洗牌以"掩盖"它,但保留真实数据。
英文:
For test purpose, I have the following table:
CREATE TABLE DANILO.TESTE2(
CPF VARCHAR2(11),
NOME VARCHAR2(100));
insert into danilo.teste2 values ('93434433443','Sousa');
insert into danilo.teste2 values ('54545454545','Ferreira');
insert into danilo.teste2 values ('99393201832','Cavalcante');
insert into danilo.teste2 values ('13902939392','Silva');
Then, I've used this answer to Shuffle cpf
column data, and it's working fine for my case.
select t2.cpf, t.nome
from (select t.*, rownum as seqnum from danilo.teste2 t) t join
(select t.*, row_number() over (order by dbms_random.value) as seqnum from danilo.teste2 t) t2 -- TABELA QUE EMBARALHA
on t.seqnum = t2.seqnum;
But how can I shuffling it and update all cpf rows?
I've tried something like it:
MERGE INTO danilo.teste2 t1
USING (
SELECT
cpf,
ROW_NUMBER() OVER (ORDER BY DBMS_RANDOM.VALUE) AS random_order
FROM danilo.teste2
) shuffled_data
ON (1 = 1)
WHEN MATCHED THEN
UPDATE SET t1.cpf = shuffled_data.cpf;
but I got ORA-30926: unable to get a stable set of rows in the source tables
What I need is to Shuffle cpf
column data to "mask" it, but leaving the real data.
答案1
得分: 1
只需在MERGE
语句的USING
子句中使用你的查询(或类似的与ROWID
伪列相关的内容):
MERGE INTO danilo.teste2 dst
USING (
select t.rid,
t2.nome
from (
SELECT ROWNUM AS rn,
ROWID AS rid
FROM danilo.teste2
) t
INNER JOIN (
SELECT nome,
ROW_NUMBER() OVER (ORDER BY DBMS_RANDOM.VALUE) AS rn
FROM danilo.teste2
) t2
ON t.rn = t2.rn
) src
ON (src.rid = dst.ROWID)
WHEN MATCHED THEN
UPDATE SET nome = src.nome;
然后,在MERGE
语句之后,表中可能(随机地)包含:
CPF | NOME |
---|---|
93434433443 | Cavalcante |
54545454545 | Sousa |
99393201832 | Ferreira |
13902939392 | Silva |
英文:
Just use your query (or something similar correlating on the ROWID
pseudo-column) in the USING
clause of a MERGE
statement:
MERGE INTO danilo.teste2 dst
USING (
select t.rid,
t2.nome
from (
SELECT ROWNUM AS rn,
ROWID AS rid
FROM danilo.teste2
) t
INNER JOIN (
SELECT nome,
ROW_NUMBER() OVER (ORDER BY DBMS_RANDOM.VALUE) AS rn
FROM danilo.teste2
) t2
ON t.rn = t2.rn
) src
ON (src.rid = dst.ROWID)
WHEN MATCHED THEN
UPDATE SET nome = src.nome;
Then, after the MERGE
statement, the table may (randomly) contain:
CPF | NOME |
---|---|
93434433443 | Cavalcante |
54545454545 | Sousa |
99393201832 | Ferreira |
13902939392 | Silva |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论