2014-10-08 64 views
0

我有這個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記錄,因爲它具有最近導入的日期。

enter image description here

回答

0

更新的答案爲任何人尋找。

僅供參考:我知道內部select中有無關代碼,但是此語句是從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  
0

添加

ORDER BY [ImportedDate] DESC 

到查詢的底部。

+0

這也將返回不需要的重複記錄,它只會將它們推到結果集的底部。不是我在找什麼。謝謝 – Tim 2014-10-08 05:21:16

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 
+0

這返回0記錄。偉大的建議爲COALESCE。任何簡化該部分的內容都是值得歡迎的。 – Tim 2014-10-08 05:19:59

+0

@Tim,這意味着table1中的第一行與table2中的任何內容都不匹配。這個問題說,那是你想要的行,但是。 – 2014-10-08 05:22:36

+0

如果你真的想要連接表中的第一行,只需將'top'移動到主查詢而不是子查詢 – 2014-10-08 05:23:52