Solved

Select and Display Duplicate Records

Posted on 2008-09-30
7
301 Views
Last Modified: 2012-05-05
I need to select and display using SQL duplicate records from the following table

bomparent  bompos
12                 1
12                  2
13                 1
13                  2
12                  1
12                  2
13                 3

I need to return
12       1
12       2
12       1
12        2

from the above table.

Thanks
0
Comment
Question by:RickCooper
  • 4
  • 3
7 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22605688
The first query will only what is duplicated, not every duplicate record
12    1
12    2


the second query will return all items that have duplicates
12    1
12    2
12    1
12    2


select bomparent,bompos

from YourTable

group by bomparent,bompos

having count(*)>1
 
 

GO
 

select a.* from YourTable

join 

(select bomparent,bompos

from YourTable

group by bomparent,bompos

having count(*)>1) b

on a.bomparent = b.bomparent

and a.bompos = b.bompos

Open in new window

0
 
LVL 1

Author Comment

by:RickCooper
ID: 22605758
Hi BrandonGalderisi
The second query is what is required as I need to delete the duplicates. However it fails with The column prefix 'a' does not match with a table name or alias name used in the query.

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22606051
oops...

forgot the alias after "YourTable" which you obviously need to replace with your actual table name.
select a.* from YourTable a

join 

(select bomparent,bompos

from YourTable

group by bomparent,bompos

having count(*)>1) b

on a.bomparent = b.bomparent

and a.bompos = b.bompos

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Closing Comment

by:RickCooper
ID: 31501550
Spot on. Thanks very much.
I now have over 17000 records to delete.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22606450
So you are deleting one of the two I assume.  And you're not on SQL 2000 by chance are you?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22606456
Sorry....

And you're not on SQL "2005" by chance are you?
0
 
LVL 1

Author Comment

by:RickCooper
ID: 22606545
I'm deleting one of the two and I'm on SQL2000
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to remove non-duplicated row 5 28
SQL Query to display duplicates ? 6 39
Query Records that don't match 8 34
SQL Error in WHERE Clause 5 42
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now