Ich habe das folgende Datenbankschema:
Product ID | Component | ...
Produkt-ID - ein Fremdschlüssel
Komponente - Teile des Produkts
Aus irgendeinem Grund haben einige Records die gleiche Produkt ID & Component. Gibt es eine SQL-Abfrage, die alle Product IDs & Components zurückgibt, die mehrere identische Komponenten haben?
Z.B. die folgende Tabelle gegeben
| Product ID | Component |
--------------------------
| 1 | c1000 |
| 1 | c1100 |
| 2 | c2000 |
| 2 | c2000 |
| 2 | c2200 |
| 3 | c3000 |
Die SQL-Abfrage sollte zurückgeben:
| Product ID | Component |
--------------------------
| 2 | c2000 |
Antworten:
2 für die Antwort № 1SELECT
ProductId,
Component
FROM
Table
GROUP BY
ProductId,
Component
HAVING
COUNT(*) > 1
2 für die Antwort № 2
SELECT ProductId, Component, count(*) Duplicates
from MyTable -- or whatever
group by ProductId, Component
having count(*) > 1
Dies zeigt Ihnen auch, wie viele doppelte Einträge es gibt.
1 für die Antwort № 3
select "Product ID", Component
from table
group by "Product ID", Component
having count(*) > 1