Click here to Skip to main content
15,885,904 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to construct an Oracle SQL query to find matching data in a table across two columns.

I have a large table with the following columns (among others):
'ModuleItemID' [Primary Key int]
'ModuleNumber' [int] column matching to a lookup table of Module Names
'PartNo' column [string]

A single ModuleNumber can be associated with many PartNo's, but each PartNo should only appear with each ModuleNumber once.

I need to root out several instances of PartNos that appear twice with the same ModuleNumber - i.e. "find all ModuleItemID rows with a particular ModuleNumber and PartNo combination where count >1" - but I'm not sure what the exact syntax is.

In the example below, I want to find duplicate rows like 1 & 3 and 7 & 9:

ModuleItemIDModuleNumberPartNo
11161-4514
21168-0056
31161-4514
42160-4514
52168-0056
63160-4514
73171-0023
83181-0565
93171-0023


Thank you!
Posted

1 solution

This should get you a list of Module/Part Numbers and how many times they occur in the table:
SELECT ModuleNumber, PartNo, COUNT(*) as ModulePartCount FROM MyTable GROUP BY ModuleNumber, PartNo

If you only want duplicates, you can add a having clause:
SELECT ModuleNumber, PartNo, COUNT(*) as ModulePartCount FROM MyTable GROUP BY ModuleNumber, PartNo HAVING COUNT(*) > 1

Is that what you are looking for? If not, please state exactly what your end result is supposed to be.
 
Share this answer
 
v2
Comments
Nostrom0 22-Jun-11 12:01pm    
The second query did it - thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900