msaccess使用多个条件将最近的匹配记录从一个表连接到另一个表

时间:2015-08-03 20:56:19

标签: sql ms-access join inner-join

根据msaccess join most recent matching record from one table to another

的答案进行扩展
SELECT c.Tag, c.DateCreated AS most_recent, c.Comment, c.Author
FROM
    (
        SELECT Tag, MAX(DateCreated) AS MaxDate 
        FROM Comments 
        GROUP BY Tag
    ) AS md
    INNER JOIN
    Comments AS c
        ON c.Tag = md.Tag AND c.DateCreated = md.MaxDate

如何使用多个条件/ n + 1条件来获得结果,例如:我想使用tag,critera1和criteria2作为单个复合键来查询状态和注释表。

编辑:我已经修改了这个问题,因为我最初的问题显然无法通过将源数据存储在一个表中来完成,因此我会问另一个问题将数据分成单独的表。此问题已被修改以匹配@DonJewett

给出的答案

表格 - 结果

+----+--------+-----------+-----------+----------------+-------------+--------+
| ID |  Tag   | Criteria1 | Criteria2 |  DateCreated   |   Comment   | Author |
+----+--------+-----------+-----------+----------------+-------------+--------+
|  6 | TAG001 | ghi       | jkl       | 25-July-2015   | Something6  | AQ     |
|  8 | TAG001 | mno       | pqr       | 23-July-2015   | Something8  | BV     |
| 13 | TAG002 | abc       | abc       | 22-June-2015   | Something13 | BV     |
| 14 | TAG001 | abc       | def       | 06-August-2015 | Something14 | AB     |
+----+--------+-----------+-----------+----------------+-------------+--------+

我的表格:

状态表

+--------+-------------+-----------+-----------+----------------+
|  Tag   |   Status    | Criteria1 | Criteria2 |   DateStatus   |
+--------+-------------+-----------+-----------+----------------+
| TAG001 | Not Started | abc       | def       | 04-August-2015 |
| TAG001 | Complete    | ghi       | jkl       | 04-August-2015 |
| TAG001 | Complete    | mno       | pqr       | 02-August-2015 |
| TAG002 | Not Started | abc       | abc       | 02-August-2015 |
+--------+-------------+-----------+-----------+----------------+

评论表:

+----+--------+-----------+-----------+----------------+-------------+--------+
| ID |  Tag   | Criteria1 | Criteria2 |  DateCreated   |   Comment   | Author |
+----+--------+-----------+-----------+----------------+-------------+--------+
|  1 | TAG001 | abc       | def       | 22-July-2015   | Something1  | JS     |
|  3 | TAG001 | abc       | def       | 23-July-2015   | Something3  | AM     |
|  6 | TAG001 | ghi       | jkl       | 25-July-2015   | Something6  | AQ     |
|  8 | TAG001 | mno       | pqr       | 23-July-2015   | Something8  | BV     |
| 12 | TAG002 | abc       | abc       | 20-June-2015   | Something12 | AZ     |
| 13 | TAG002 | abc       | abc       | 22-June-2015   | Something13 | BV     |
| 14 | TAG001 | abc       | def       | 06-August-2015 | Something14 | AB     |
+----+--------+-----------+-----------+----------------+-------------+--------+

我尝试使用AND,但它没有用。

SELECT c.Tag, c.DateCreated AS most_recent, c.Comment, c.Author
FROM
    (
        SELECT Tag, MAX(DateCreated) AS MaxDate 
        FROM Comments 
        GROUP BY Tag
    ) AS md
    INNER JOIN
    Comments AS c
        ON c.Tag = md.Tag AND c.DateCreated = md.MaxDate AND status.critera1 = c.criteria1 AND status.criteria2 = c.criteria2

完整的答案:

MaxCommentDateQuery

SELECT Tag, MAX(DateCreated) AS MaxDate, Criteria1, Criteria2
FROM Comments
GROUP BY Tag, Criteria1, Criteria2;

ResultQuery

SELECT c.Tag, c.DateCreated AS most_recent, c.Comment, c.Author
FROM MaxCommentDateQuery AS md INNER JOIN Comments AS c ON (md.Tag = c.Tag) AND (md.MaxDate = c.DateCreated);

ResultQuery(现在有状态)

SELECT DISTINCT c.*
FROM Status
INNER JOIN (MaxCommentDateQuery AS md
INNER JOIN Comments AS c
ON (md.Tag = c.Tag) AND (md.MaxDate = c.DateCreated) AND (c.criteria1 = md.criteria1) AND (c.criteria2 = md.criteria2))
ON (Status.Tag = c.Tag) AND (Status.Criteria1 = c.Criteria1) AND (Status.Criteria2 = c.Criteria2);

2 个答案:

答案 0 :(得分:0)

您的查询中没有状态表,因此您无法在查询中使用它。我建议将子查询分解为自己的单独查询,然后您就可以使用UI进行编辑。

<强> MaxCommentDateQuery

SELECT Tag, MAX(DateCreated) AS MaxDate 
    FROM Comments 
    GROUP BY Tag

<强> ResultQuery

SELECT c.Tag, c.DateCreated AS most_recent, c.Comment, c.Author
FROM MaxCommentDateQuery AS md
INNER JOIN Comments AS c
    ON c.Tag = md.Tag AND c.DateCreated = md.MaxDate

一旦发现这些问题,您就可以使用UI向ResultQuery添加Status。加入标签,然后您应该可以在您的标准中使用状态字段。

版本1

这是Tag,Criteria1,Criteria2加入评论和状态的复合键。结果将限制为最大评论日期。您可能会发现这会过度限制您的结果。

ResultQuery(现在有状态)

SELECT c.*
FROM Status INNER JOIN (MaxCommentDateQuery AS md INNER JOIN Comments AS c 
ON (md.MaxDate = c.DateCreated) AND (md.Tag = c.Tag)) 
ON (Status.Criteria2 = c.Criteria2) 
    AND (Status.Criteria1 = c.Criteria1) 
    AND (Status.Tag = c.Tag);

版本2

如果您需要与最长评论日期相关的条件,请使用此版本:

MaxCommentDateQuery(现状态)

SELECT Tag, MAX(DateCreated) AS MaxDate, Criteria1, Criteria2
FROM Comments
GROUP BY Tag, Criteria1, Criteria2;

ResultQuery(现在有状态)

SELECT DISTINCT c.*
FROM Status
INNER JOIN (MaxCommentDateQuery AS md
INNER JOIN Comments AS c
ON (md.Tag = c.Tag) AND (md.MaxDate = c.DateCreated) 
     AND (c.criteria1 = md.criteria1) AND (c.criteria2 = md.criteria2))
ON (Status.Tag = c.Tag) AND (Status.Criteria1 = c.Criteria1) 
     AND (Status.Criteria2 = c.Criteria2);

答案 1 :(得分:0)

试试这个:

noHKB