Remove repeated lines in all aspects on Oracle

Asked

Viewed 5,873 times

0

In a column REGISTER I have two lines repeated in all aspects:

NOME   ID    EMAIL           SEXO    
JOAO   1234  [email protected]  M
JOAO   1234  [email protected]  M

How to compare these two rows, check if all columns are identical and delete one of them in order to result:

NOME   ID    EMAIL           SEXO    
JOAO   1234  [email protected]  M
  • You have no unique identifier for your table?

  • If for George’s question (above) it is no, can you change the table structure? I don’t know about Oracle, but in SQL Server there is no way to remove just one of them - you need a unique identifier for each record.

  • I am voting to close this question as out of scope because it is unclear whether AP intends to do this in a Select or there is duplicate data that it wants to delete.

3 answers

3


Understanding that you want to delete duplicate records from the database but, keeping one (1) of them, I suggest using the rowid.

The ROWID is a pseudo-column that returns the address of a record, structurally you won’t see it, but, it’s there.
It uniquely identifies a row within a table.

In the script below we have:

  • A sub-query seeking all minors rowid of each repeated grouping;

  • A command delete in all records that are not in the minimum grouping made in the sub-query;

Script:

delete from cadastro
where rowid not in
     (select min(rowid)
      from cadastro
group by nome, id, email, sexo);

With this, duplicates will be eliminated leaving at least one of each.

Note: You should never store one rowid in its tables as a key value (unique).

2

In sql server, the method below works. In oracle I think it is similar:

select *
into #table
from(   select nome = 'JOAO', id=1234, email='[email protected]', sexo='m'
        union all
        select nome = 'JOAO', id=1234, email='[email protected]', sexo='m'
        union all
        select nome = 'manuel', id=1237, email='[email protected]', sexo='m') t

   ;WITH cte AS (
        SELECT  *, 
                row_number() OVER(PARTITION BY nome,id,email,sexo ORDER BY nome,id,email,sexo) AS rn
        FROM #table
    )
    DELETE cte WHERE rn > 1

select * 
from #table

drop table #table

1

  • But he needs to remove the repeated lines leaving only one.

  • @But that’s what the distinct does. Or I must have misunderstood (the question and your comment).

  • 1

    What I understood that he wants to remove from the database these lines "the most" - ie, unify these records.

  • His theory makes sense, but is unlikely. Then he would have two records with the same ID? Of course it’s possible, but I don’t believe it’s the case.

  • There’s every crazy system out there that doesn’t hurt to doubt. Let’s wait for more details.

  • Yes, you’re right. I’ll also wait for more details.

  • What’s this link for? @Motta

  • @jbueno , post removed

  • @Motta I didn’t really understand, rs. But all right.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.