Solved

Need help with an SQL query

Posted on 2008-06-20
5
161 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL + Default Value + Row Above 13 54
combine data in sql 1 29
Delete from table 6 36
Query 14 0
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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