5

I have found many answers on selecting non-distinct rows where they group by a singular column, for example, e-mail. However, there seems to have been issue in our system where we are getting some duplicate data whereby everything is the same except the identity column.

SELECT DISTINCT 
      COLUMN1,
      COLUMN2,
      COLUMN3,
      ...
      COLUMN14
  FROM TABLE1

How can I get the non-distinct rows from the query above? Ideally it would include the identity column as currently that is obviously missing from the distinct query.

2
  • 1
    Can you provide sample data for this? I'm having a hard time understanding what your expected result is. Commented Sep 18, 2015 at 12:23
  • It's basically people's contact details, where there are double entries for some people and the only difference is the ID column Commented Sep 18, 2015 at 12:26

3 Answers 3

14
select COLUMN1,COLUMN2,COLUMN3 
from TABLE_NAME 
group by COLUMN1,COLUMN2,COLUMN3 
having COUNT(*) > 1
Sign up to request clarification or add additional context in comments.

1 Comment

also dont select the id column as it would be always distinct .
1
With _cte (col1, col2, col3, id) As
    (
        Select cOl1, col2, col3, Count(*)
          From mySchema.myTable
          Group By Col1, Col2, Col3
          Having Count(*) > 1
    )
Select t.*
From _Cte As c
Join mySchema.myTable As t
On c.col1 = t.col1
And c.col2 = t.col2
And c.col3 = t.col3

1 Comment

The descriptions are not very descriptive but it helped me. Basically first it does the aggregation to find duplicates. Then it finds whole rows from the table based on a comparison of multiple columns from the aggregation (the inner join part) and gives out just the columns of the original table.
1
SELECT * FROM 
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY COL 1, COL 2, .... COL N ORDER BY COL M
) RN
FROM TABLE_NAME 
)T
 WHERE T.RN>1

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.