如何在多个列中查找重复项?
所以我想要做下面这样的sql代码:
select s.id, s.name,s.city from stuff s group by s.name having count(where city and name are identical) > 1
为了产生以下内容(但是忽略只有名字或城市匹配的地方,它必须在两列上):
id name city 904834 jim London 904835 jim London 90145 Fred Paris 90132 Fred Paris 90133 Fred Paris
对name
和city
重复id
:
select s.id, t.* from [stuff] s join ( select name, city, count(*) as qty from [stuff] group by name, city having count(*) > 1 ) t on s.name = t.name and s.city = t.city
SELECT name, city, count(*) as qty FROM stuff GROUP BY name, city HAVING count(*)> 1
像这样的事情会做的伎俩。 不知道性能,所以做一些testing。
select id, name, city from [stuff] s where 1 < (select count(*) from [stuff] i where i.city = s.city and i.name = s.name)
你必须自己join东西,匹配名称和城市。 然后按数字分组。
select s.id, s.name, s.city from stuff s join stuff p ON ( s.name = p.city OR s.city = p.name ) group by s.name having count(s.name) > 1
找出多于一列的重复logging,最好的方式自我join表
select * from [stuff] s join ( select name, city from [stuff] group by name, city having count(*) > 1 ) t on s.name = t.name and s.city = t.city
给定一个70列的登台表,只有4个表示重复,这段代码将返回违规的列:
SELECT COUNT(*) ,LTRIM(RTRIM(S.TransactionDate)) ,LTRIM(RTRIM(S.TransactionTime)) ,LTRIM(RTRIM(S.TransactionTicketNumber)) ,LTRIM(RTRIM(GrossCost)) FROM Staging.dbo.Stage S GROUP BY LTRIM(RTRIM(S.TransactionDate)) ,LTRIM(RTRIM(S.TransactionTime)) ,LTRIM(RTRIM(S.TransactionTicketNumber)) ,LTRIM(RTRIM(GrossCost)) HAVING COUNT(*) > 1
。