下面是我當前顯示所有記錄的 MS Access SQL。但是,如果有重復的賬戶記錄,它應該只顯示具有最新日期的記錄 ComputationPeriodStartingDate
資料集示例。應回傳所有非重復項,帳戶 1005 是唯一的重復項,僅應包括日期為 2021 年 12 月 1 日的重復項
| 帳戶 | 計算期間開始日期 | 姓 | 類別 |
|---|---|---|---|
| 10001 | 12/1/2021 | 肯特 | 積極的 |
| 10005 | 12/1/2021 | 納什頓 | 積極的 |
| 10005 | 6/1/2021 | 納什頓 | 積極的 |
| 10011 | 1/1/2022 | 斯蒂爾 | 積極的 |
| 10015 | 12/1/2021 | 富有的 | 積極的 |
我不知道如何讓我當前的 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;
預期結果
| 帳戶 | 計算期間開始日期 | 姓 | 類別 |
|---|---|---|---|
| 10001 | 12/1/2021 | 肯特 | 積極的 |
| 10005 | 12/1/2021 | 納什頓 | 積極的 |
| 10011 | 1/1/2022 | 斯蒂爾 | 積極的 |
| 10015 | 12/1/2021 | 富有的 | 積極的 |
uj5u.com熱心網友回復:
嘗試使用子查詢:
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
子查詢將為ComputationPeriodStartingDate每個回傳最大值Account。然后,我們對表連接它Account和max_date。
注意:您沒有提到表名。因此相應地更新表名。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/359371.html
上一篇:左連接后表中的重復記錄
