我有這個t-sql腳本,我需要修改以確保table1中沒有重複的行。我想通過抓取[ImportedDate]列中具有最新日期的行來做到這一點。我可以得到一些指導嗎? 謝謝從表中選擇最新導入日期的行
更新澄清 我需要選擇table1中匹配table2中的所有行。但是,每個表中有多個記錄實例。所以我也需要確保我只從table1中爲每個[MIN]數字(最新版本)提取1條記錄。因此,例如當前的結果集是20008條記錄,我應該通過清除重複數據來獲得更少的結果。我在想它需要一個內在的選擇。
SELECT mr.Id
,mr.F2FResolved
,mr.F2FIgnore
,mr.[F2FIgnore Always]
,REPLACE(LTRIM(RTRIM([MIN])), '-', '') AS [MIN]
,LTRIM(RTRIM(mr.BorrowerLastName)) AS BorrowerLastName
,LTRIM(RTRIM(mr.BorrowerFirstName)) AS BorrowerFirstName
,LTRIM(RTRIM(mr.BorrowerSSN)) AS BorrowerSSN
,LTRIM(RTRIM(mr.PropertyStreet)) AS PropertyStreet
,LTRIM(RTRIM(mr.PropertyZip)) AS PropertyZip
,LTRIM(RTRIM(mr.NoteAmount)) AS NoteAmount
,LTRIM(RTRIM(mr.LienType)) AS LienType
FROM table1 mr
INNER JOIN table2 d ON LTRIM(RTRIM(MERSMin)) = REPLACE(LTRIM(RTRIM(mr.[MIN])), '-', '')
WHERE (((mr.[F2FResolved] IS NULL
OR mr.[F2FResolved] = 0
)
AND (d.[F2FResolved] IS NULL
OR d.[F2FResolved] = 0
)
)
OR ((mr.[F2FIgnore Always] IS NULL
OR mr.[F2FIgnore Always] = 0
)
AND d.[F2FIgnore Always] IS NULL
OR d.[F2FIgnore Always] = 0
)
OR ((mr.[F2FIgnore] IS NULL
OR mr.[F2FIgnore] = 0
)
AND d.[F2FIgnore] IS NULL
OR d.[F2FIgnore] = 0
)
)
AND ((mr.[F2FProcessed] IS NULL
OR mr.[F2FProcessed] = 0
)
AND (d.[F2FProcessed] IS NULL
OR d.[F2FProcessed] = 0
)
)
在圖片中你會看到Id = 65759和52413是針對同一個人的。我只需要檢索65759記錄,因爲它具有最近導入的日期。
這也將返回不需要的重複記錄,它只會將它們推到結果集的底部。不是我在找什麼。謝謝 – Tim 2014-10-08 05:21:16