hyphenpipe
asked on
Need help with an SQL query
I need to query a table in my database that contains client records. I would like to see just the people in this table who have the same first name AND same last name and thier distinct ID number.
In otherwords the results should look something like:
ID, last_name, first_name
-------------------------- -----
23 Jones, James
45 Jones, James
67 Stewart, Martha
122 Stewart, Martha
Thanks in advance!
In otherwords the results should look something like:
ID, last_name, first_name
--------------------------
23 Jones, James
45 Jones, James
67 Stewart, Martha
122 Stewart, Martha
Thanks in advance!
try this:
select id, first_name, last_name
from yourtablename t
join
(
select first_name, last_name
from yourtablename
group by first_name, last_name
) a on t.first_name = a.first_name and t.last_name = a.last_name
select id, first_name, last_name
from yourtablename t
join
(
select first_name, last_name
from yourtablename
group by first_name, last_name
) a on t.first_name = a.first_name and t.last_name = a.last_name
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm sorry, the first and last name don't have to be equal but their whole name, ie find all the clients whose name are James Jones, Martha Stewart, etc.
ASKER
chapmandew - yours worked, I just needed to reformat it as:
select id, t.first_name, t.last_name
from yourtablename t
join
(
select first_name, last_name
from yourtablename
group by first_name, last_name
having count(*)> 1
) a on t.first_name = a.first_name and t.last_name = a.last_name
select id, t.first_name, t.last_name
from yourtablename t
join
(
select first_name, last_name
from yourtablename
group by first_name, last_name
having count(*)> 1
) a on t.first_name = a.first_name and t.last_name = a.last_name
Select ID,Last_Name, First_Name From MyTable Where Last_Name = First_Name
Should do that.
Regards,
TimCottee