Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecting one record from duplicates

Author  Topic 

delpi767
Starting Member

11 Posts

Posted - 2009-09-10 : 17:16:30
I have a table "residents" which has a field lot number and a field email.

I also have a table "address" that has uniq lot numbers that relate to the "residents" table.

Some of the homes (lots) will have more than one email address but I want to send only one email to each address so I need to return each distinct lot number and the first email address associated with that lot number.

Do I just need to return all residents and loop through the recordset or can SQL do this?



Regards,

-dmd-

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-09-10 : 17:20:14
SELECT LotNumber, min(EmailAddress) Email FROM Residents GROUP BY LotNumber

There's no concept of "first" in a relational database, so the Min() or Max() function would be used for email address.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-13 : 13:03:11
by first did you mean first in alphabetical manner? if not, you need to specify how you determine order.
Go to Top of Page
   

- Advertisement -