msaccess join most recent matching record from one

2019-08-09 02:03发布

The result I want.

+--------+-------------+------------+--------+
|  Tag   | most_recent |  Comment   | Author |
+--------+-------------+------------+--------+
| TAG001 | 2015-07-23  | Something3 | AM     |
| TAG002 | 2015-07-25  | Something5 | BN     |
+--------+-------------+------------+--------+

The tables I have:

Status Table

+--------+-------------+------------+
|  Tag   |   Status    | DateStatus |
+--------+-------------+------------+
| TAG001 | Not Started |            |
| TAG002 | Complete    | 2015-07-23 |
+--------+-------------+------------+

Comments Table

+----+--------+-------------+------------+--------+
| ID |  Tag   | DateCreated |  Comment   | Author |
+----+--------+-------------+------------+--------+
|  1 | TAG001 | 2015-07-22  | Something1 | JS     |
|  2 | TAG002 | 2015-07-23  | Something2 | JS     |
|  3 | TAG001 | 2015-07-23  | Something3 | AM     |
|  4 | TAG002 | 2015-07-23  | Something4 | AS     |
|  5 | TAG002 | 2015-07-25  | Something5 | BN     |
+----+--------+-------------+------------+--------+

I've tried 4 different queries, each getting progressively more complicated, but still not working.

The queries I have tried:

Query 1)

SELECT Comments.[Tag], Max(Comments.[DateCreated]) AS most\_recent
FROM Comments
GROUP BY Comments.[Tag];

Result 1)

+--------+-------------+
|  Tag   | most_recent |
+--------+-------------+
| TAG001 | 2015-07-23  |
| TAG002 | 2015-07-25  |
+--------+-------------+

Just gives me the most recent date, but no values.

Query 2)

SELECT Comments.[Tag], Max(Comments.[DateCreated]) AS most\_recent
FROM Comments
GROUP BY Comments.[Tag];

Result 2)

+--------+-------------+------------+
|  Tag   | most_recent |  Comment   |
+--------+-------------+------------+
| TAG001 | 2015-07-22  | Something1 |
| TAG001 | 2015-07-23  | Something3 |
| TAG002 | 2015-07-23  | Something2 |
| TAG002 | 2015-07-23  | Something4 |
| TAG002 | 2015-07-25  | Something5 |
+--------+-------------+------------+

Now I see all the information I want, but I cannot filter for the most recent.

I tried DISTINCT, but it didn't work.

Query 3)

Modified from here: MYSQL - Join most recent matching record from one table to another

SELECT Status.\*,Comments.\*
FROM Status S
LEFT JOIN Comments C ON S.tag = C.tag
JOIN(SELECT x.tag, MAX(x.DateCreated) AS MaxCommentDate FROM Comments x
GROUP BY x.tag) y ON y.tag = x.tag AND y.MaxCommentDate = x.DateCreated

Result: Syntax error (missing operator) in query expression

Query 4)

Modified from here: Left Join to most recent record

SELECT
Status.\*,Comments.\*
FROM Status S
LEFT JOIN
(
Comments C
INNER JOIN
(
SELECT
x.tag, MAX(x.DateCreated) AS MaxCommentDate
FROM
Comments x
GROUP BY
x.tag
)
y
ON y.tag = x.tag
AND y.MaxCommentDate = x.DateCreated
)
ON S.tag = C.tag;

Result: Syntax Error on JOIN

Not having much luck...thanks in advanced.

Thanks.

1条回答
仙女界的扛把子
2楼-- · 2019-08-09 02:54

The following seems to work for me in Access 2010:

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
查看更多
登录 后发表回答