在查询中查找最新结果 - ms访问

时间:2016-07-19 10:51:55

标签: date ms-access

我有一个访问数据库,我正在尝试确定查询中的最新结果。我想知道任何项目的最新贷款是否完整。 e.g。

tbl_Loans

LoanID    Equipment Number  Start Date  Return Date Expr:IsOnLoan
123       78457             16/05/2001  19/06/2003  Loan Complete
124       79557             17/07/2016              Loan in Progress
125       78457             03/06/2016              Loan in Progress
126       79630             01/01/2015  01/01/2016  Loan Complete
127       79360             02/03/2016              Loan in Progress
128       78457             01/01/2016  01/03/2016  Loan Complete

Expr:IsOnLoan是IIF:

IsOnLoan: IIf([Loans]![Return Date] Is Null,"Loan Active","Loan Complete")

我想在查询的另一列(可能是单独的查询)中返回每个设备编号的最新结果,即

Loan ID    Equipment Number   Start Date  Return Date Expr:IsOnLoan
125        78457              03/06/2016              Loan in Progress
127        79360              02/03/2016              Loan in Progress
128        78457              01/01/2016  01/03/2016  Loan Complete

微软的支持页面会让我使用顶级'但是我见过的其他人已经使用了内部联接,我无法让他们中的任何一个工作。

任何帮助非常感谢

由于

1 个答案:

答案 0 :(得分:0)

处理此问题的一种方法是在INNER JOIN表和子查询之间使用tbl_Loans,该子查询标识每个设备编号的最新日期。

SELECT t1.LoanID,
       t1.[Equipment Number],
       t1.[Start Date],
       t1.[Return Date],
       t1.[Expr:IsOnLoan]
FROM tbl_Loans AS t1
INNER JOIN
(
    SELECT [Equipment Number], MAX([Start Date]) AS [Start Date]
    FROM tbl_Loans
    GROUP BY [Equipment Number]
) AS t2
    ON t1.[Equipment Number] = t2.[Equipment Number] AND
       t1.[Start Date] = t2.[Start Date]