需要在多列中找到包含重复数据的行
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 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
有什么想法吗?提前谢谢!
答案 0 :(得分: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;
答案 1 :(得分: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
)
答案 2 :(得分: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
)
答案 3 :(得分: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