[英]SQL duplicates with different primary keys
I have a table in this form: 我有一张这样的表格:
id | firstname | lastname
---+-----------+----------
1 | alex | marti
2 | mark | finger
3 | alex | marti
4 | ted | port
Need to return the firstname
, lastname
duplicates in this form: 需要返回
firstname
, lastname
以这种形式重复:
1 | alex | marti
3 | alex | marti
I tried doing select firstname, lastname from t group by firstname, lastname having count(*) > 1
but that will return something like 我试着
select firstname, lastname from t group by firstname, lastname having count(*) > 1
但这将返回类似
firstname | lastname
----------+----------
mark | finger
alex | marti
ted | port
And I need the id
of the duplicates but of course select id, firstname, lastname from t group by id, firstname, lastname
won't work. 我需要重复的
id
,但是select id, firstname, lastname from t group by id, firstname, lastname
行不通的。
Any ideas? 有任何想法吗? Thanks.
谢谢。
select a.* from t a,
(select first, last from t group by first, last having count(*) > 1) b
where a.first = b.first and a.last = b.last
You need to aggregate the id. 您需要汇总ID。 If you need only the ID of one of them, for, say, deletion, you could do:
如果仅需要其中之一的ID(例如,删除),则可以执行以下操作:
select max(id) id, firstname, lastname from t group by firstname, lastname having count(*) > 1
If you want both id's and know there will never be more than 2, you could do the following: 如果您同时需要两个ID,并且知道不会超过2个,则可以执行以下操作:
select min(id) minid, max(id) maxid, firstname, lastname from t group by firstname, lastname having count(*) > 1
If you want all duplicates, along with their id's, you'll have to use a derived table, as in Nitin Midha's answer. 如果要所有重复项及其ID,则必须使用派生表,如Nitin Midha的回答。
Select Id, First_Name, Last_Name
FROM
(
Select Id, First_Name, Last_Name,
Count() Over (Partition By First_Name,Last_Name) Count
From Emp
) AS T
Where T.Count > 1
select id, firstname, lastname
from table t
where exists (select 1
from table t2
where t2.firstname = t.firstname
and t2.lastname = t.lastname
and t2.id <> t.id)
I ran into the same problem and this is what I did to resolve it. 我遇到了同样的问题,这就是我要解决的问题。 First I identified the dups with the following query:
首先,我通过以下查询确定了公仔:
SELECT COUNT(*) as num, ID, Firstname, Lastname FROM TableA GROUP BY ID, Firstname, Lastname;
Then I created a temp table. 然后我创建了一个临时表。 called TempTableA Which had the same columns as TableA and extra Column called Dups, you will see why further.
名为TempTableA的列与TableA的列相同,而额外的列称为Dups,您将进一步了解原因。
then I did the following insert: 然后我做了以下插入:
INSERT INTO TempTableA(Dups, ID, Firstname, Lastname) SELECT COUNT(*) as num, ID, Firstname, Lastname FROM TableA GROUP BY ID, Firstname, Lastname having count(*)>=1;
By now you might know why we added an extra column called dups. 到目前为止,您可能知道为什么我们添加了一个称为dups的额外列。 anywho..
任何人..
After that I did the following delete statement: 之后,我执行了以下删除语句:
DELETE FROM TableA Where ID NOT IN (SELECT t.ID FROM TempTableA t);
And presto that did the work for me remove the rest of the dups. 之前为我所做的工作便消除了其余的公仔。
Its not a one step process but it did do the job right. 它不是一步一步的过程,但是确实做得很好。
NOTE: you need to change the tableA to the correct name that you have as well as the column names in order for it to work. 注意:您需要将tableA更改为您拥有的正确名称以及列名称,以使其起作用。 Let me know if you run into any issues.
让我知道您是否遇到任何问题。
--Remove Duplicate Rows with different ID SQL SERVER
CREATE TABLE #TempTable
(
Id int,
Firstname varchar(20),
Lastname varchar(20)
)
INSERT INTO #TempTable( Id, Firstname, Lastname) SELECT min(Id)as Id, Firstname, Lastname
FROM UserTable
GROUP BY Firstname, Lastname
delete from UserTable where Id not in(select Id from #TempTable)
drop #TempTable
you can do the following to show all the id column values 您可以执行以下操作以显示所有id列值
SELECT GROUP_CONCAT(DISTINCT id SEPARATOR ',') AS ids, firstname, lastname FROM t GROUP BY firstname, lastname HAVING COUNT(*) > 1
This should show something like this: 这应该显示如下内容:
ids | firstname | lastname
----+-----------+----------
1,3 | alex | marti
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.