views:

18

answers:

1

Working in MS Access 2003 SP3: I have a query that I am running to find what 'cars' were sold with a date after the delivery date. I have thousands of rows. When it is all said and done, I want to just have a handful of rows for each 'car' and then the oldest date. Any suggestions?

CAR          DATE ORDERED   DATE DELIVERED  CUSTOMER NUMBER DATE SOLD
FORD MUSTANG    20061002             20080413            ABC123          20080422
FORD MUSTANG    20061002             20080413            ABC124          20080429
CHEVY IMPALA    20061002             20080413            ABC125          20080505
A: 

This could be better if you had an ID field:

DELETE 
FROM Cars
WHERE Cars.DATESOLD Not In (
             SELECT TOP 5 DateSold 
             FROM Cars c 
             WHERE c.Car=Cars.Car 
             ORDER BY DateSold DESC) 
And Cars.DATESOLD Not In (
             SELECT TOP 1 DateSold 
             FROM Cars c 
             WHERE c.Car=Cars.Car 
             ORDER BY DateSold)

If there are duplicate dates, you will end up with more than 5 records.

Remou