我在下面有这个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记录,因为它具有最新的导入日期。
答案 0 :(得分:0)
添加
ORDER BY [ImportedDate] DESC
到查询的底部。
答案 1 :(得分:0)
您需要子查询才能获取最新日期。我更喜欢CTE,因为它使代码更清晰。
通过在所有检查中使用coalesce()
,无论是零还是空,您还可以使事情变得更简单。
with latestdate (latest) as (
select max([ImportedDate]) from table1
)
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 latestdate ON [ImportedDate] = latest
INNER JOIN table2 d ON LTRIM(RTRIM(MERSMin)) = REPLACE(LTRIM(RTRIM(mr.[MIN])), '-', '')
WHERE (
( coalesce(mr.[F2FResolved],0) = 0
AND coalesce(d.[F2FResolved] ,0) = 0
)
OR ( coalesce(mr.[F2FIgnore Always],0) = 0
AND coalesce(d.[F2FIgnore Always] ,0) = 0
)
OR ( coalesce(mr.[F2FIgnore],0) = 0
AND coalesce(d.[F2FIgnore] ,0) = 0
)
)
AND coalesce(mr.[F2FProcessed],0) = 0
AND coalesce(d.[F2FProcessed] ,0) = 0
答案 2 :(得分:0)
更新了所有正在寻找的答案。
仅供参考:我知道内部选择中存在无关代码,但此语句是从c#应用程序动态生成的,其中用户可以选择要查看的列。所以列选择代码只生成一次并添加到string.Format语句两次以输出格式化的sql语句。
SELECT DISTINCT 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 ( SELECT mr.Id
,mr.F2FResolved
,mr.F2FIgnore
,mr.[F2FIgnore Always]
,REPLACE(LTRIM(RTRIM(mr.[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
,mr.F2FProcessed
,ROW_NUMBER() OVER ( PARTITION BY mr.[MIN] ORDER BY mr.[ImportedDate] DESC ) AS rn
FROM Table1 mr
) AS mr
INNER JOIN Table2 d ON LTRIM(RTRIM(MERSMin)) = REPLACE(LTRIM(RTRIM(mr.[MIN])), '-', '')
WHERE ( ( COALESCE(mr.[F2FResolved], 0) = 0
AND COALESCE(d.[F2FResolved], 0) = 0
)
OR ( COALESCE(mr.[F2FIgnore Always], 0) = 0
AND COALESCE(d.[F2FIgnore Always], 0) = 0
)
OR ( COALESCE(mr.[F2FIgnore], 0) = 0
AND COALESCE(d.[F2FIgnore], 0) = 0
)
)
AND COALESCE(mr.F2FProcessed, 0) = 0
AND COALESCE(d.F2FProcessed, 0) = 0
AND rn = 1