Solved

Select and Display Duplicate Records

Posted on 2008-09-30
7
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stored Proc - Rewrite 42 77
Syntax error creating JSON recordset 4 43
Error in sql query statment. 21 70
Create a Calendar table 29 44
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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