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
 Max Date for each customer?

Author  Topic 

ldrenning
Starting Member

23 Posts

Posted - 2008-07-07 : 22:38:56
I am still new to SQL and would appreciate any help.

I have a table with customers and another table that tracks service dates (one to many). I need to run a query that will show the most recent service date for each customer.

customers:
id (primary,integer)
first_name
last_name
address
city
state
zip
frequency_of_service (integer)

service_tracker:
id (primary,integer)
customer_id (foreign key)
date_of_service (date/time)
paid (boolean)
description (text)

I have tried this query, but it doesn't give the desired results.

SELECT Max(service_tracker.date_of_service) AS MaxOfdate_of_service
FROM customers LEFT JOIN service_tracker ON customers.ID = service_tracker.customer_id;

Any help would be great!

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-07 : 22:59:28
[code]SELECT customers.ID, MAX(service_tracker.date_of_service) AS MaxOfdate_of_service
FROM customers LEFT JOIN service_tracker ON customers.ID = service_tracker.customer_id
GROUP BY customers.ID[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ldrenning
Starting Member

23 Posts

Posted - 2008-07-07 : 23:41:30
Thanks! This worked perfectly.
Go to Top of Page
   

- Advertisement -