1

需要在多列中查找具有重复数据的行

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

应该可以像dis一样返回..

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

有任何想法吗?提前致谢!

4

4 回答 4

2

下面的查询比@TimSchmelter 的解决方案重复得多,但如果您碰巧在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;
于 2012-11-13T07:02:18.553 回答
1

你可以GROUP BY多列。但是由于您想查找所有重复项,因此可以使用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
)
于 2012-11-13T07:04:56.957 回答
1

这适用于我尝试过的所有 SQL 服务器(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
)
于 2012-11-13T07:43:03.763 回答
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
于 2013-04-16T17:02:20.353 回答