查询根据最早的日期消除多行

时间:2010-07-22 19:44:49

标签: ms-access date rows

在MS Access 2003 SP3中工作:我正在运行一个查询,以查找在交付日期之后出售的“汽车”。我有成千上万的行。完成所有操作后,我想为每辆“汽车”设置一些行,然后是最早的日期。有什么建议吗?

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

1 个答案:

答案 0 :(得分:0)

如果你有一个ID字段,这可能会更好:

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)

如果有重复日期,您最终将获得超过5条记录。