This is a very basic SQL question. What I've got is a database for a garage with two tables: CARS and SERVICES
. Each car is brought in for servicing once in a while, so each car can have multiple SERVICES records. Each record in SERVICES
has a foreign key pointing to the related car, a date field and some additional info fields.
What I want is to get a list of cars with all the info from the most recent servicing. If I simply do
SELECT C.ID, S.*
FROM CARS C
JOIN SERVICES S ON S.CAR_ID = C.ID
Then I get a list of all cars with all services for each car (the Cartesian product of the two data sets). I'm using MS SQL Server.