T-SQL to return latest customer email

I have a two table database:


I want to return a dataset of all customers and just their latest active email (they can have more than one).

In the Email table there are 5 fields:

If EmailStatus is 1 (active) I want to order all their associated addresses by DateEntered DESC and grab the top 1. Thus I only return their "latest" email address.

I assume this is needing some kind of subquery.

I'd like the query to simply return (one for each active customer):

Since they can technically have more than one email address in our system, I am getting back 3 rows for the same customer (if they happen to have 3 active email addresses in our system).

How can I query the email table as part of my overall query to return just 1 row per customer?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this:

select c.customerid,c.CustomerName,e.emailaddress
from customer c
join email e
on c.customerid = e.customerid
and emailid = (select top 1 emailid from email where customerid = c.customerid and emailstatus=1 order by dateentered desc)
tivowatcherAuthor Commented:
Just to be clear, what happens if the customer has no associated email address?

I am following you on the JOIN and the subquery and I bet that works, but should this be a LEFT JOIN to ensure I get the customer info even if they have no email?

Correct.  If the customer can possibly have no email, it should be a left join.  I missed that in your original post.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tivowatcherAuthor Commented:
Got it (I think).

Will this work? It seems to in my testing:

SELECT     c.CustomerID, e.EmailAddress
FROM         Customer c LEFT OUTER JOIN
                      Email e ON c.CustomerID = e.CustomerID AND e.EmailID =
                          (SELECT     TOP 1 emailid
                            FROM          email
                            WHERE      customerid = c.customerid
                            ORDER BY dateentered DESC)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.