2010-12-08 6 views
0

Ich habe folgendes Datenbankschema:SQL-Select-Zeilen, die (teilweise) haben Daten dupliziert

Product ID | Component | ... 

Product ID - ein Fremdschlüssel

Component - Teile des Produkts

Für einige Arcane Grund eine Anzahl von Datensätzen haben die gleiche Produkt ID & Komponente. Gibt es eine SQL-Abfrage, die alle Produkt-ID & Components zurückkehren würde, die mehrere identische Komponenten haben?

z. angesichts der folgenden Tabelle

| 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  | 
+0

Die verwendeten DBMS ist Oracle 8 –

Antwort

2
SELECT 
    ProductId, 
    Component 
FROM 
    Table 
GROUP BY 
    ProductId, 
    Component 
HAVING 
    COUNT(*) > 1 
+2

Danke ... Ich kann sehen, wo ich falsch ging, ich nur die ProductId in der Gruppe durch Klausel hatte! –

+0

@TK: Sie sind herzlich willkommen! –

2
SELECT ProductId, Component, count(*) Duplicates 
from MyTable -- or whatever 
group by ProductId, Component 
having count(*) > 1 

Dies wird auch zeigen Ihnen, wie viele doppelte Einträge gibt.

+0

sollte die erste Linie 'SELECT ProductId, Component, count (*) als Dubletten'? –

1
select "Product ID", Component 
from table 
group by "Product ID", Component 
having count(*) > 1 
Verwandte Themen