访问 SQL 查询以仅显示重复记录的最近日期
Access SQL Query to display the most recent date only for duplicate records
下面是我当前显示所有记录的 MS Access SQL。但是,如果有重复的账户记录,它应该只显示最新日期 ComputationPeriodStartingDate
的记录
数据集示例。所有非重复项都应 returned,帐户 1005 是唯一的重复项,其中只有日期为 12/1/2021 的副本应包含在 return
中
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
尝试使用子查询:
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
return 最大值 ComputationPeriodStartingDate
。然后我们在 Account
和 max_date
.
上加入 table
注意: Table您没有提到姓名。因此相应地更新 table 名称。
下面是我当前显示所有记录的 MS Access SQL。但是,如果有重复的账户记录,它应该只显示最新日期 ComputationPeriodStartingDate
的记录数据集示例。所有非重复项都应 returned,帐户 1005 是唯一的重复项,其中只有日期为 12/1/2021 的副本应包含在 return
中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 |
尝试使用子查询:
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
return 最大值 ComputationPeriodStartingDate
。然后我们在 Account
和 max_date
.
注意: Table您没有提到姓名。因此相应地更新 table 名称。