SQL Duplicate Query

I have a table with duplicates rows other than an auto-generated identity field.

eg. MyTable_ID, MyTable_COL1, MyTable_COL2, MyTable_COL3.

I would like to get the values for MyTable_ID for each row where MyTable_COL1, MyTable_COL2, MyTable_COL3 are duplicates.

Any ideas?
DaveChoiceTechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

:with CTE as
(
select MyTable_ID, MyTable_COL1, MyTable_COL2, MyTable_COL3,row_number() over (PARTITION BY MyTable_COL1, MyTable_COL2, MyTable_COL3 order by MyTable_ID) RN from MyTable
)

SELECT * FROM CTE WHERE rn > 1

this will give only duplicate records which if you want to delete you can write

delete FROM CTE WHERE rn > 1
0
DaveChoiceTechAuthor Commented:
Vikas, your answer is close but not quite what I'm looking for. I need both MyTable_ID's where the other columns are duplicates. In your query the first of these rows will have an RN of and the duplicate(s)will have RN of 2 or greater. If I row has duplicates I need the MyTable_ID's for the first row as well as the other duplicates.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vikas GargBusiness Intelligence DeveloperCommented:
Remove the where clause and you will get all the records and from the rn column field you can come to know the original which has rn=1 and duplicates which has rn > 1
0
DaveChoiceTechAuthor Commented:
Yes but it will also include the records with rn=1 which do not have duplicates in the remaining colums. I need only the rows that have an rn=1 and thier duplicates with rn> 1
0
PortletPaulfreelancerCommented:
remove the comments from the first 2 lines for a delete, otherwise run a a select to test
adjust the top(n) to suit, this might be useful if there are lots to delete
--DELETE TOP (10000) FROM m
--      FROM MyTable m
SELECT * FROM MyTable
      INNER JOIN (
            SELECT
                  MIN(MyTable_ID) AS MyTable_ID
                , MyTable_COL1
                , MyTable_COL2
                , MyTable_COL3
            FROM MyTable WITH (NOLOCK)
            GROUP BY
                  MyTable_COL1
                , MyTable_COL2
                , MyTable_COL3
            HAVING COUNT(*) > 1
      ) AS dups
            ON m.MyTable_ID > dups.MyTable_ID  --IMPORTANT: use only greater than here
            AND m.MyTable_COL1 = dups.MyTable_COL1
            AND m.MyTable_COL2 = dups.MyTable_COL2
            AND m.MyTable_COL3 = dups.MyTable_COL3

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
You can self-join the cte

...
Selected ...
From cte c1
Join cte c2
 On c2.col1 = c1.col1
...
 And c2.rn > 1
Where c1.rn =1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaveChoiceTechAuthor Commented:
Brilliant! Thanks a lot.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Guys - I just knocked an article titled SQL Server Delete Duplicate Rows Solutions that covers the above comments and a wompload of others regarding duplicate rows.

If you have some time, please give it a read and let me know if you like it.

Thanks.
Jimbo
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.