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

ディスのように戻れるはず..

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

これは、私が試したすべての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 に答える
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 に答える
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 に答える