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

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

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

fiddle

英文:

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

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:

确定