开发者

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:

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

firstname | lastname
----------+----------
mark      | finger
alex      | marti
ted       | port

And I need the id of the duplicates but of course select id, firstname, lastnam开发者_Python百科e from t group by id, firstname, lastname won't work.

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. If you need only the ID of one of them, for, say, deletion, you could do:

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:

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.


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)


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


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.

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. 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. 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

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜