4
I have the following situation: I have a table ItensVendas
where I need to apply a composite key to the fields VendedorId
and ProdutoId
, however I have some duplicated data, ie I can not apply the composite key unless delete duplicate data.
|-----------|----------|
VendedorId ProdutoId
|-----------|----------|
1 600
|-----------|----------|
2 500
|-----------|----------|
1 600
|-----------|----------|
4 600
|-----------|----------|
The query I made to bring the duplicate data was this:
SELECT t.*
FROM ITENSVENDA s
JOIN
(
SELECT VENDEDORID, PRODUTOID
FROM ITENSVENDA
GROUP BY VENDEDORID, PRODUTOID
HAVING COUNT(*) > 1
) t
ON s.VENDEDORID = t.VENDEDORID AND s.PRODUTOID = t.PRODUTOID
I’m not getting to delete duplicate data, also not being sure that my query is really effective.
Have you tried your select so?
SELECT VendedorId,ProdutoId 
FROM ITENSVENDA
GROUP BY ProdutoId
HAVING COUNT(t.VendedorId) > 1
– Marconi