1

Need to find the rows with duplicates data in a multiple columns

hID     sid    lid    aid    did     shid    soid         date
------------------------------------------------------------------
0       123    jkn    rbr    tm      asd     hjk       10/11/2008
1       563    kjh    jkh    dfsd    we      hjk       11/12/2010
2       131    jklj   jkjk   adsd    vcv     asd       01/01/2012
3       656    hjkh   hjkh   dasda   fas     fads      03/26/2012
4       123    jkn    rbr    tm      asd     hjk       09/09/2012

Should be able to return like dis one..

hID     sid    lid    aid    did     shid    soid         date
------------------------------------------------------------------
0       123    jkn    rbr    tm      asd     hjk       10/11/2008
4       123    jkn    rbr    tm      asd     hjk       09/09/2012

Any ideas? thanks in advance!

4 Answers 4

2

The query below is much more repetitive than @TimSchmelter's solution, but yields a measurable performance advantage for large tables with relatively few duplicates if you happen to have an index on sid, lid, aid, did, shid, soid.

SELECT t.* FROM t INNER JOIN (
  SELECT sid, lid, aid, did, shid, soid, COUNT(1) AS count FROM t
  GROUP BY sid, lid, aid, did, shid, soid HAVING COUNT(1) > 1
) tt ON t.sid=tt.sid AND t.lid=tt.lid AND t.aid=tt.aid AND t.did=tt.did AND t.shid=tt.shid AND t.soid=tt.soid;
Sign up to request clarification or add additional context in comments.

Comments

1

You can GROUP BY multiple columns. But since you want to find all duplicates you can use EXISTS:

SELECT
   hID, sid, lid, aid, did, shid, soid, date
FROM TableName t1
WHERE EXISTS
(
   SELECT 1 FROM TableName t2
   WHERE t2.hid <> t1.hid
   AND  t2.sid  = t1.sid   
   AND  t2.lid  = t1.lid    
   AND  t2.aid  = t1.aid   
   AND  t2.did  = t2.did
   AND  t2.shid = t2.shid   
   AND  t2.soid = t2.soid
)

Comments

1

This works in all SQL servers I tried (SQLFiddle):

SELECT * FROM t
WHERE (sid,lid,aid,did,shid,soid) IN (
    SELECT sid,lid,aid,did,shid,soid
    FROM t
    Group By 1,2,3,4,5,6
    HAVING count(1) > 1
)

Comments

0
;with tempTable as
(
select *, row_number() over(partition by sid, lid, aid, did, shid, soid order by hid asc )  as rowid
from #table)
select *
from tempTable
where rowid>1

1 Comment

this will return the duplicated row only.

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.