Solved

Need help with an SQL query

Posted on 2008-06-20
5
172 Views
Last Modified: 2010-03-19
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!
0
Comment
Question by:hyphenpipe
  • 2
  • 2
5 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 21831031
Hello hyphenpipe,

Select ID,Last_Name, First_Name From MyTable Where Last_Name = First_Name

Should do that.

Regards,

TimCottee
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21831040
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
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21831046
whoops..one change:

select id, first_name, 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
0
 
LVL 6

Author Comment

by:hyphenpipe
ID: 21831071
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.
0
 
LVL 6

Author Comment

by:hyphenpipe
ID: 21831088
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
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now