我已经研究了这个问题并学习了不同的SQL技术近一周。我试图在Microsoft Access的列表框中显示结果。我有列SQLIDofLoggers, JobNumber, DateDeployed, DateReturned, AHULocation, AccessPoint
和两个组合框cboLoggerType and cboOrderBy
来过滤这些结果(我使用VBA对cboOrderBy
进行了解决方法,这就是为什么它不在发布的SQL中)。
我想要做的是为每个不同的SQLIDofLogger
仅提取一条记录。要确定每个SQLIDofLogger
的几个记录中的哪一条记录,我想通过DateReturn
编辑它们并选择最新但我不想显示他们{{1}的任何记录是空的,即他们还在工作上
这是我目前提供正确信息的SQL,它只显示每个记录器的多条记录:
DateReturn
我试图尽可能地清理这段代码。我知道这不是理想的,但是随着我的进展,我只是不断添加点点滴滴,它“有效”。我会发布我试图得到的最后一点方程式,但没有一个工作(显然哈哈)。我不知道这方面的最佳做法(SELECT
(SELECT TOP 1 tblLoggers.LoggerID FROM tblLoggers WHERE tblLoggers.ID = tblUse.IDofLogger) AS SQLIDofLogger,
(SELECT TOP 1 tblProjectList.JobNumber FROM tblProjectList WHERE( tblProjectList.ID = tblUse.IDofProject)) AS expr1,
tblUse.DateDeployed, tblUse.DateReturned, tblUse.AHULocation, tblUse.AccessPoint
FROM tblUse
WHERE (
(IIf([Forms]![frmByType].[cboLoggerType]<>'(ALL)',
((SELECT TOP 1 tblLoggers.LoggerType FROM tblLoggers WHERE tblLoggers.ID = tblUse.IDofLogger ORDER BY ID)=[Forms]![frmByType].[cboLoggerType]),
'*')<>False
) AND (
(tblUse.DateReturned) In (SELECT MAX(tblUse.DateReturned) FROM tblUse GROUP BY tblUse.IDofLogger))
)
GROUP BY tblUse.DateDeployed, tblUse.DateReturned, tblUse.AHULocation, tblUse.AccessPoint, tblUse.IDofProject, tblUse.IDofLogger
ORDER BY 1, tblUse.AHULocation;
,有些GROUP BY
,JOIN
等等,所以我愿意接受任何建议。