2010-12-08 39 views
0

選擇行我有以下的數據庫模式:SQL已經(部分)重複數據

Product ID | Component | ... 

產品ID - 一個外鍵

組件 - 該產品的部分

對於一些祕法導致許多記錄具有相同的產品ID &組件。是否有SQL查詢將返回具有多個相同組件的所有產品ID的&組件?

E.g.下表給出

| Product ID | Component | 
-------------------------- 
| 1   | c1000  | 
| 1   | c1100  | 
| 2   | c2000  | 
| 2   | c2000  | 
| 2   | c2200  | 
| 3   | c3000  | 

的SQL查詢應返回:

| Product ID | Component | 
-------------------------- 
| 2   | c2000  | 
+0

使用的DBMS是Oracle 8 – 2010-12-08 14:40:27

回答

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

謝謝......我可以看到我出錯的地方,我只有Group by子句中的ProductId! – 2010-12-08 14:46:40

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

這也將告訴你有多少重複的條目有。

+0

應的第一行是 '選擇產品編號,組分,COUNT(*)爲重複'? – 2010-12-08 14:47:20

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