Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
Update where exists delete row where doesn't

I can update fine and I can delete fine, but I want to do both at once.

I have a table ORIG like this, column names are

ref,fname,lname,add1,add2,add3,add4

A1  a  b  c  d  h  j
S2  f  d  s  e  y  t
B3  j  f  s  e  o  p

Where the first column is unique

then another table DATA like this

ref,fname,lname,add1,add2

A1  b  c  d  e
B3  k  g  h  t

I want to update the first table using the second and delete any rows that don't have the unique

So the end result needs to be table ORIG like below

A1  b  c  d  e  h  j
B3  k  g  h  t  o  p

Am I able to accomplish this in one go?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I might approach this using CTEs:

with u as (
      update orig
          set b = d.b, . . .
          from data d
          where d.a = orig.a
      returning *
     )
delete from orig
    where not exists (select 1 from u where u.a = d.a);

The first CTE updates the rows. The second one does the delete.

over 4 years ago · Santiago Trujillo Relatório

0

You can use a correlated subquery for this:

delete
from orig o
where not exists (
        select 1
        from data d
        where d.col1 = o.col1
        )
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda