对列进行行间洗牌并更新。

huangapple go评论158阅读模式
英文:

Shuffle a column between rows and update

问题

为了测试目的,我有以下表格:

  1. CREATE TABLE DANILO.TESTE2(
  2. CPF VARCHAR2(11),
  3. NOME VARCHAR2(100));
  4. insert into danilo.teste2 values ('93434433443','Sousa');
  5. insert into danilo.teste2 values ('54545454545','Ferreira');
  6. insert into danilo.teste2 values ('99393201832','Cavalcante');
  7. insert into danilo.teste2 values ('13902939392','Silva');

然后,我使用了这个答案来对cpf列的数据进行洗牌,对于我的情况来说,它运行良好。

  1. select t2.cpf, t.nome
  2. from (select t.*, rownum as seqnum from danilo.teste2 t) t join
  3. (select t.*, row_number() over (order by dbms_random.value) as seqnum from danilo.teste2 t) t2 -- TABELA QUE EMBARALHA
  4. on t.seqnum = t2.seqnum;

但是,我如何对所有的cpf行进行洗牌和更新呢?

我尝试了以下方法:

  1. MERGE INTO danilo.teste2 t1
  2. USING (
  3. SELECT
  4. cpf,
  5. ROW_NUMBER() OVER (ORDER BY DBMS_RANDOM.VALUE) AS random_order
  6. FROM danilo.teste2
  7. ) shuffled_data
  8. ON (1 = 1)
  9. WHEN MATCHED THEN
  10. 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:

  1. CREATE TABLE DANILO.TESTE2(
  2. CPF VARCHAR2(11),
  3. NOME VARCHAR2(100));
  4. insert into danilo.teste2 values ('93434433443','Sousa');
  5. insert into danilo.teste2 values ('54545454545','Ferreira');
  6. insert into danilo.teste2 values ('99393201832','Cavalcante');
  7. 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.

  1. select t2.cpf, t.nome
  2. from (select t.*, rownum as seqnum from danilo.teste2 t) t join
  3. (select t.*, row_number() over (order by dbms_random.value) as seqnum from danilo.teste2 t) t2 -- TABELA QUE EMBARALHA
  4. on t.seqnum = t2.seqnum;

But how can I shuffling it and update all cpf rows?

I've tried something like it:

  1. MERGE INTO danilo.teste2 t1
  2. USING (
  3. SELECT
  4. cpf,
  5. ROW_NUMBER() OVER (ORDER BY DBMS_RANDOM.VALUE) AS random_order
  6. FROM danilo.teste2
  7. ) shuffled_data
  8. ON (1 = 1)
  9. WHEN MATCHED THEN
  10. 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伪列相关的内容):

  1. MERGE INTO danilo.teste2 dst
  2. USING (
  3. select t.rid,
  4. t2.nome
  5. from (
  6. SELECT ROWNUM AS rn,
  7. ROWID AS rid
  8. FROM danilo.teste2
  9. ) t
  10. INNER JOIN (
  11. SELECT nome,
  12. ROW_NUMBER() OVER (ORDER BY DBMS_RANDOM.VALUE) AS rn
  13. FROM danilo.teste2
  14. ) t2
  15. ON t.rn = t2.rn
  16. ) src
  17. ON (src.rid = dst.ROWID)
  18. WHEN MATCHED THEN
  19. UPDATE SET nome = src.nome;

然后,在MERGE语句之后,表中可能(随机地)包含:

CPF NOME
93434433443 Cavalcante
54545454545 Sousa
99393201832 Ferreira
13902939392 Silva

fiddle

英文:

Just use your query (or something similar correlating on the ROWID pseudo-column) in the USING clause of a MERGE statement:

  1. MERGE INTO danilo.teste2 dst
  2. USING (
  3. select t.rid,
  4. t2.nome
  5. from (
  6. SELECT ROWNUM AS rn,
  7. ROWID AS rid
  8. FROM danilo.teste2
  9. ) t
  10. INNER JOIN (
  11. SELECT nome,
  12. ROW_NUMBER() OVER (ORDER BY DBMS_RANDOM.VALUE) AS rn
  13. FROM danilo.teste2
  14. ) t2
  15. ON t.rn = t2.rn
  16. ) src
  17. ON (src.rid = dst.ROWID)
  18. WHEN MATCHED THEN
  19. 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

fiddle

huangapple
  • 本文由 发表于 2023年8月8日 21:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定