Solved

SQL - Find duplicate records in in two columns

Posted on 2014-09-14
2
896 Views
Last Modified: 2014-09-14
Hi Experts,
I would like to find multiple records, where the value in the Emp_Id column and the WeekRequested column is the same.
(I'd like to know if the same employee requested the same week more than once)
The query below doesn't return the expected result. How can it be modified to achieve my goal? Thanks for your help.
SELECT Emp_Id, WeekRequested
FROM Auto_Table
GROUP BY Emp_Id, WeekRequested
HAVING COUNT(*) > 1
ORDER BY Emp_Id

Open in new window

0
Comment
Question by:romsom
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40321864
Give this a whirl...
SELECT Emp_Id, WeekRequested, COUNT(WeekRequested) as the_count
FROM Auto_Table
GROUP BY Emp_Id, WeekRequested
HAVING COUNT(WeekRequested) > 1
ORDER BY Emp_Id, WeekRequested

Open in new window

btw I just kicked out an article titled SQL Server Delete Duplicate Rows Solutions that would be a good read for the many ways to hide-show-delete duplicate rows.   If you like it please it the 'Yes' button next to 'Was this article helpful'?
0
 

Author Closing Comment

by:romsom
ID: 40321885
Thank you, this is perfect. The article is also very useful. I managed to delete all duplicates with your method.
;with a as (
   SELECT Emp_Id, WeekRequested, 
      row_number() OVER (partition by Emp_Id, WeekRequested ORDER BY WeekRequested) as row_number 
   FROM Auto_Table 
)
DELETE FROM a WHERE row_number > 1

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 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