我正在使用ms-access,我有2个表tblAssets和tblauditlog,我试图获取资产状态为6(表示已出售)的所有资产的最新审计日志。这两个表之间的关系是tblassets.assetid = tblauditlog.logaspectid,我只获得了已售出资产的所有审核日志,但我正在寻找最后一条记录。我有一个日期字段,但是当我使用Max(tblauditlog.logtimestamp)时会抛出错误
我尝试使用
SELECT <br>
r.assetid, <br>
r.assetcategory, <br>
r.assetstatus, <br>
sub2.logmessage, <br>
sub2.logtimestamp <br>
FROM <br>
tblAssets as r <br>
LEFT JOIN <br> <br>
(SELECT <br>
sub1.logaspectid, <br>
a2.logMessage, <br>
a2.logtimestamp <br>
FROM <br>
( <br>
SELECT <br>
a1.logaspectid, <br>
Max(a1,logtimestamp) AS MaxOfTimeStamp <br>
FROM tblauditlog AS a1 <br>
GROUP BY a1.logaspectid ) AS sub1 <br>
INNER JOIN tblauditlog as a2 <br>
ON <br>
(sub1.MaxOfTimeStamp = a2.logTimestamp) AND (sub1.logaspectid = a2.logaspectid)) AS sub2 <br>
ON r.assetid = sub2.logaspectid ;
上面给我一个错误:
无法执行查询。使用多值的无效操作或语法 字段
这就是我要返回的多条记录
SELECT tblAssets.assetID, tblAssets.assetCategory, tblAssets.assetModel,
tblAssets.assetStatus, tblAuditLog.logAspectID, tblAuditLog.logMessage,
tblAuditLog.logTimeStamp
FROM tblAssets
LEFT JOIN tblAuditLog ON tblAssets.assetID = tblAuditLog.logAspectID
WHERE (((tblAssets.assetStatus)=6)
AND ((tblAuditLog.logTimeStamp)>=#1/1/2017#));
我只想出售所有资产状态= 6的最后记录的日志。
答案 0 :(得分:0)
如果我理解正确,则可以使用相关的子查询来获取最新的审核日志记录。然后,您可以检查其是否具有所需的状态:
SELECT . . .
FROM tblAssets as a INNER JOIN
tblauditlog as al
ON a.assetid = al.logaspectid
WHERE al.logtimestamp = (SELECT MAX(al2.logtimestamp)
FROM tblauditlog as al2
WHERE al2.logaspectid = al.logaspectid
) AND
al.assetstatus = 6 ;