Solved

Need help with an SQL query

Posted on 2008-06-20
5
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…

732 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