【问题标题】:Access SQL Query to display the most recent date only for duplicate records访问 SQL 查询以仅显示重复记录的最近日期
【发布时间】:2021-12-25 07:47:15
【问题描述】:

下面是我当前显示所有记录的 MS Access SQL。但是,如果存在重复的帐户记录,则应仅显示具有最晚日期 ComputationPeriodStartingDate 的记录

数据集示例。所有不重复的都应退回,帐户 1005 是唯一的重复,只有 2021 年 12 月 1 日的副本应包含在退货中

Account ComputationPeriodStartingDate LastName Categories
10001 12/1/2021 Kent Active
10005 12/1/2021 Nashton Active
10005 6/1/2021 Nashton Active
10011 1/1/2022 Steele Active
10015 12/1/2021 Rich Active

我不知道如何正确过滤当前的 SQL。

SELECT [TDS LOANS].Account, [TDS Escrow Projections].ComputationPeriodStartingDate, [TDS LOANS].LastName, [TDS LOANS].Categories
FROM [TDS Escrow Projections] INNER JOIN [TDS LOANS] ON [TDS Escrow Projections].LoanRecID = [TDS LOANS].RecID
WHERE ((([TDS LOANS].PrinBal)<>0))
ORDER BY [TDS LOANS].Account, [TDS Escrow Projections].ComputationPeriodStartingDate DESC;

预期结果

Account ComputationPeriodStartingDate LastName Categories
10001 12/1/2021 Kent Active
10005 12/1/2021 Nashton Active
10011 1/1/2022 Steele Active
10015 12/1/2021 Rich Active

【问题讨论】:

    标签: sql ms-access


    【解决方案1】:

    尝试使用子查询:

    SELECT a.Account, a.ComputationPeriodStartingDate, a.LastName, a.Categories
        FROM
          (
    SELECT [TDS LOANS].Account, [TDS Escrow Projections].ComputationPeriodStartingDate, [TDS LOANS].LastName, [TDS LOANS].Categories
        FROM [TDS Escrow Projections] INNER JOIN [TDS LOANS] ON [TDS Escrow Projections].LoanRecID = [TDS LOANS].RecID
        WHERE ((([TDS LOANS].PrinBal)<>0))
        ORDER BY [TDS LOANS].Account, [TDS Escrow Projections].ComputationPeriodStartingDate DESC
    ) 
        as a
            INNER JOIN
            (
    SELECT Account, MAX(ComputationPeriodStartingDate) as max_date
            FROM
        (SELECT [TDS LOANS].Account, [TDS Escrow Projections].ComputationPeriodStartingDate, [TDS LOANS].LastName, [TDS LOANS].Categories
        FROM [TDS Escrow Projections] INNER JOIN [TDS LOANS] ON [TDS Escrow Projections].LoanRecID = [TDS LOANS].RecID
        WHERE ((([TDS LOANS].PrinBal)<>0))
        ORDER BY [TDS LOANS].Account, [TDS Escrow Projections].ComputationPeriodStartingDate DESC
    )
            GROUP BY Account) as b
            ON a.Account=b.Account AND a.ComputationPeriodStartingDate = b.max_date
    

    对于每个Account,子查询将返回ComputationPeriodStartingDate 的最大值。然后我们将它与Accountmax_date 上的表格连接起来。

    注意:您没有提到表名。因此相应地更新表名。

    【讨论】:

    • 我确实为我的查询提供了表名。这是我在我查看的任何示例(例如您提供的示例)中难以理解的部分。
    • 这将是我的子查询 SELECT Max([TDS Escrow Projections].ComputationPeriodStartingDate) AS MaxOfComputationPeriodStartingDate, [TDS Escrow Projections].LoanRecID FROM [TDS Escrow Projections] GROUP BY [TDS Escrow Projections]。 LoanRecID;
    • 我刚刚更新了查询。我刚刚用您的整个查询替换了 TABLE。让我知道这是否有效。
    • 非常感谢!它返回正是我需要的。我需要一些时间来理解它,但我会尝试!
    • 我可以帮助您了解我的查询逻辑:首先,我将您的查询用作表格。接下来,我正在为子查询中的每个 Account 计算 max(ComputationPeriodStartingDate)。然后将该表与 Account 和 max(date) 的子查询返回的表连接起来。查询可能看起来很混乱,因为我将您的查询用作表格。
    猜你喜欢
    • 2023-03-31
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多