Ho il seguente schema del database:
Product ID | Component | ...
ID prodotto: una chiave esterna
Componente: parti del prodotto
Per alcuni motivi arcani, un numero di record ha lo stesso ID e componente del prodotto. Esiste una query SQL che restituisce tutti i componenti e i codici identificativi del prodotto, che hanno più componenti identici?
Per esempio. data la seguente tabella
| Product ID | Component |
--------------------------
| 1 | c1000 |
| 1 | c1100 |
| 2 | c2000 |
| 2 | c2000 |
| 2 | c2200 |
| 3 | c3000 |
La query SQL dovrebbe restituire:
| Product ID | Component |
--------------------------
| 2 | c2000 |
risposte:
2 per risposta № 1SELECT
ProductId,
Component
FROM
Table
GROUP BY
ProductId,
Component
HAVING
COUNT(*) > 1
2 per risposta № 2
SELECT ProductId, Component, count(*) Duplicates
from MyTable -- or whatever
group by ProductId, Component
having count(*) > 1
Questo ti mostrerà anche quante voci duplicate ci sono.
1 per risposta № 3
select "Product ID", Component
from table
group by "Product ID", Component
having count(*) > 1