Question : query to return distinct email id's, joined to their most recent customer record

Struggling to get this working in SQL 2000.

I have a table of customers linked to a table of customer contacts (name, phone, email, etc.).  There are duplicate email id's, some of which are linked to the same customer record, and some are linked to different customer records.  I need to return a list of distinct email ids and for each one, include the most recently saved customer record it's linked to.

contacts contains customer_code and email fields
customers contains customer_code and save_time fields

Surprising to me, that "select distinct(email), customer_code from ......." returns the same email more than once provided it matches more than one customer_code.

Thanks for any help-

Answer : query to return distinct email id's, joined to their most recent customer record

@acperkins how right you are - cheers :) in the case I would use something like

select CC.Email, C.id
from Contact CC, Customer C
where C.id = CC.Customer
and not exists -- test the a more recent record doesn't exist
(
  select 1
  from Contact CC1, Customer C1
  where C1.id = CC1.Customer
  and CC1.id <> C.id
  and C1.Save_Time > C.Save_Time
)
Random Solutions  
 
programming4us programming4us