Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with DISTINCT

Status
Not open for further replies.

mitzi2

Programmer
May 9, 2005
2
0
0
US
I have a table with names and addresses of people. There are many people from the same city. I need to take only one from NYC, only one from Chicago, only one from LA etc. I don’t care who are the ones who would not be selected. DISTINCT would not resolve my problem. Currently I am doing it programmatically with a short SP using cursor. I am confident that there is a better way to accomplish it. Can someone help me with this?

 
You need some way to choose an arbitrary one from any group of duplicates. It's easiest to use Min or Max on a column which uniquely identifies each row (such as an identity column) as a way to select the one you won't delete. Look at my query in the thread maswien already pointed you to and you should be able to easily modify it to get what you want.

-------------------------------------
Only the unvirtuous can be dutiful, for the virtuous already perform duty's requirements by inclination and for pleasure. Where there is no pain, no disinclination, there is no duty.
- Erik E
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top