Suppose I have this "Product" table
id number
categoria
nombre varchar
precio number
I need to make a query in oracle that I delete the duplicate data that match the name and price, is it possible with a query without using a procedure?
Suppose I have this "Product" table
id number
categoria
nombre varchar
precio number
I need to make a query in oracle that I delete the duplicate data that match the name and price, is it possible with a query without using a procedure?
The most optimal way is:
DELETE FROM table WHERE rowid not in (SELECT MIN(rowid)
FROM table GROUP BY "nombre", "precio" );
You could do the following:
select min(id) id,
nombre,
precio
from productos p
left join (select nombre, precio, count(1)
from productos
group by nombre, precio
having count(1) > 1
) dup
on dup.nombre = p.nombre
and dup.precio = p.precio
group by
nombre,
precio
The select
more internal would bring the precios
and nombres
that were duplicated, then we make join
with the products to have the id
of the duplicates, and in the last select
, we group and we get the lowest id
for each nombre
and precio
. That I understand should be the id
to eliminate ( delete productos where id in (...)
). Logically doing so we would not be contemplating if for example there are more than two duplicates, in that case we could keep the max(id)
that would be the one that should "survive" and eliminate the others that coincide in nombre
and precio
and not have said ìd