【发布时间】:2018-12-18 13:27:00
【问题描述】:
我有三张桌子:
- 我的产品及其 ID 和功能。
- 是一个表格,其中包含我的产品的处理方式,包括处理 ID、方法和日期。处理是分批进行的,因此有一个交叉表
- 带有产品 ID 和治疗 ID 以及治疗成功的布尔值。
每种产品都可以进行多种不同的处理,因此存在多对多的关系。我现在想在产品表 (1.) 中为每个产品添加一个值,该值显示其最近成功处理的方法(如果有的话)。
我进行了一个查询,按产品 ID 对交叉表的条目进行分组,但我不知道如何显示最后一次处理的方法和日期。
table 1:
| productID | size | weight | height | ... |
|-----------|:----:|-------:|--------|-----|
| 1 | 13 | 16 | 9 | ... |
| 2 | 12 | 17 | 12 | ... |
| 3 | 11 | 15 | 15 | ... |
| ... | ... | ... | ... | ... |
table 2:
| treatmentID | method | date |
|-------------|:--------:|-----------:|
| 1 | dye blue | 01.02.2016 |
| 2 | dye red | 01.02.2017 |
| 3 | dye blue | 01.02.2018 |
| ... | ... | ... |
table 3:
| productID | treatmentID | success |
|-----------|:-----------:|--------:|
| 1 | 1 | yes |
| 1 | 2 | yes |
| 1 | 3 | no |
| ... | ... | ... |
我需要表 1 是这样的:
table 1:
| productID | size | weight | height | latest succesful method |
|-----------|:----:|-------:|--------|-------------------------|
| 1 | 13 | 16 | 9 | dye red |
| 2 | 12 | 17 | 12 | ... |
| 3 | 11 | 15 | 15 | ... |
| ... | ... | ... | ... | ... |
我的查询:
SELECT table3.productID, table2.method
FROM table2 INNER JOIN table3 ON table2.treatmentID = table3.treatmentID
GROUP BY table3.productID, table2.method
HAVING (((table3.productID)=Max([table2].[date])))
ORDER BY table3.productID DESC;
但这并不只显示一个(最近的)条目,而是所有条目。
【问题讨论】:
-
您能否也添加一些示例数据(包括表名和字段标题)和预期输出。 This 适合格式化表格。我们是否也可以查看您已经构建的查询的 SQL。
-
是的,试图概括一切。感谢您的编辑。
标签: database ms-access ms-access-2016