Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda