2009-09-24 72 views
0

DocID |更新日期| DocName根據時間更新刪除重複記錄

8245376 | 8/26/2009 1:07:01 PM | Doc1
8245376 | 8/26/2009 4:07:04 PM | Doc1
8245376 | 8/26/2009 7:07:01 PM | Doc2
8245376 | 8/26/2009 2:07:03 PM | Doc2
646681 | 8/26/2009 1:07:01 PM | Doc3
263272 | 8/26/2009 1:07:01 PM | Doc4
8245376 | 8/26/2009 1:07:01 PM | Doc5
8245376 | 8/26/2009 2:07:10 PM | Doc5
8245376 | 8/26/2009 1:07:01 PM | Doc6
8245376 | 8/26/2009 10:07:01 PM | Doc6
1903875 | 8/26/2009 1:07:01 PM | Doc7

從上面的記錄集,如何檢索唯一的行,根據日期更新,即檢索最近更新的記錄?

感謝, 格塔

+0

整體的最近更新的記錄?最近更新的每個DocID? – GilaMonster 2009-09-24 05:39:10

+0

每個DocID最近更新的記錄! – Geethapriya 2009-09-24 05:49:27

回答

2

SELECT * FROM表t1加入(選擇文檔ID,最大爲從被docID表組dateupdated(dateupdated))爲t2上t1.docid = t2.docid和t1.dateupdated = T2 .dateudated

+0

完美!謝謝:) – Geethapriya 2009-09-24 06:04:16

0

這假定您只想保留最近更新的每個DocId。我正在使用SQL Server DELETE語句,但爲了使用WHERE NOT IN子句,我必須通過串聯相關字段來作弊。還有其他的方法,但這是相當簡潔的。

DELETE 
FROM Docs 
WHERE CAST(DocId AS VARCHAR) + '_' + 
     CAST(Date_Updated AS VARCHAR) NOT IN 
(
    SELECT CAST(DocId AS VARCHAR) + '_' + 
     CAST(MAX(Date_Updated) AS VARCHAR) 
    From Docs 
    GROUP BY DocId 
) 
0
select distinct DocID, max("Date Updated") from mytable group by DocID 
+0

在這種情況下無法顯示docname ... – Geethapriya 2009-09-24 06:01:07

+0

編輯(從select子句刪除DocName) – 2009-09-24 06:08:31