查询以根据最旧日期消除多行

发布于 2024-09-10 16:28:44 字数 451 浏览 3 评论 0原文

在 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙厌世 2024-09-17 16:28:45

如果您有 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 条记录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文