tags:

views:

79

answers:

4

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, lastname from t group by id, firstname, lastname won't work.

Any ideas? Thanks.

A: 

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

Nitin Midha
I'm certain that this is non-standard SQL
Tim Drisdelle
Use of Analytical Functions, which comes with SQL 2005 and above.
Nitin Midha
+2  A: 
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
jspcal
This is one of the ways to do it. Basically all the _correct_ solutions require you to join a result set of duplicates (on first name, last name) back to the original table.
Craig Young
A: 

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.

David Hedlund
Thank you very much
Luca Matteis
NO! #1 is clearly not what was asked, and #2 only supports 2 duplicates, yet it is trivial to obtain the information OP was asking for no matter how many duplicates.
Craig Young
i'll agree that #2 would rarely be applicable, but in cases where it is, that will give better performance than using a derived table, so i wanted to show that alternative as well. i'll trust OP to handle the judgement of whether or not it fits the description. as for #1: it may be true that it doesn't meet the exact requirements of the original question, but 9 out of 10 times when questions such as this one pops up, what is *wanted* (regardless of what is *asked for*) is a simple way to delete these duplicate items, and for that purpose, #1 will suffice a lot of the times.
David Hedlund
(continued) i think you'll agree that actually helping the user is what this site is *really* all about; that will not always come down to simply answering the question, because the question may not be phrased just right. i provided a solution that i suspected would resolve the problems that Lucas was facing, and the accept button and the comment seems to indicate that it did indeed.
David Hedlund
+1  A: 
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)
Steve De Caux
This one also works nicely... Basically select any rows where you can find any duplicate with a different ID.
Craig Young