ms access 将最近的匹配记录从一个 table 连接到另一个
msaccess join most recent matching record from one table to another
我想要的结果
+--------+-------------+------------+--------+
| Tag | most_recent | Comment | Author |
+--------+-------------+------------+--------+
| TAG001 | 2015-07-23 | Something3 | AM |
| TAG002 | 2015-07-25 | Something5 | BN |
+--------+-------------+------------+--------+
我有的表:
状态 Table
+--------+-------------+------------+
| Tag | Status | DateStatus |
+--------+-------------+------------+
| TAG001 | Not Started | |
| TAG002 | Complete | 2015-07-23 |
+--------+-------------+------------+
评论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 |
+----+--------+-------------+------------+--------+
我尝试了 4 种不同的查询,每一种都变得越来越复杂,但仍然无法正常工作。
我尝试过的查询:
查询 1)
SELECT Comments.[Tag], Max(Comments.[DateCreated]) AS most\_recent
FROM Comments
GROUP BY Comments.[Tag];
结果 1)
+--------+-------------+
| Tag | most_recent |
+--------+-------------+
| TAG001 | 2015-07-23 |
| TAG002 | 2015-07-25 |
+--------+-------------+
只给我最近的日期,但没有值。
查询 2)
SELECT Comments.[Tag], Max(Comments.[DateCreated]) AS most\_recent
FROM Comments
GROUP BY Comments.[Tag];
结果 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 |
+--------+-------------+------------+
现在我看到了我想要的所有信息,但我无法筛选出最新的信息。
我试过 DISTINCT,但没用。
查询 3)
从这里修改: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
结果:查询表达式中存在语法错误(缺少运算符)
查询 4)
从这里修改:
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;
结果:JOIN 语法错误
运气不太好...先谢谢了。
谢谢。
以下内容似乎适用于 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
我想要的结果
+--------+-------------+------------+--------+ | Tag | most_recent | Comment | Author | +--------+-------------+------------+--------+ | TAG001 | 2015-07-23 | Something3 | AM | | TAG002 | 2015-07-25 | Something5 | BN | +--------+-------------+------------+--------+
我有的表:
状态 Table
+--------+-------------+------------+ | Tag | Status | DateStatus | +--------+-------------+------------+ | TAG001 | Not Started | | | TAG002 | Complete | 2015-07-23 | +--------+-------------+------------+
评论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 | +----+--------+-------------+------------+--------+
我尝试了 4 种不同的查询,每一种都变得越来越复杂,但仍然无法正常工作。
我尝试过的查询:
查询 1)
SELECT Comments.[Tag], Max(Comments.[DateCreated]) AS most\_recent
FROM Comments
GROUP BY Comments.[Tag];
结果 1)
+--------+-------------+ | Tag | most_recent | +--------+-------------+ | TAG001 | 2015-07-23 | | TAG002 | 2015-07-25 | +--------+-------------+
只给我最近的日期,但没有值。
查询 2)
SELECT Comments.[Tag], Max(Comments.[DateCreated]) AS most\_recent
FROM Comments
GROUP BY Comments.[Tag];
结果 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 | +--------+-------------+------------+
现在我看到了我想要的所有信息,但我无法筛选出最新的信息。
我试过 DISTINCT,但没用。
查询 3)
从这里修改: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
结果:查询表达式中存在语法错误(缺少运算符)
查询 4)
从这里修改: 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;
结果:JOIN 语法错误
运气不太好...先谢谢了。
谢谢。
以下内容似乎适用于 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