2012-11-16 142 views
0

我想刪除表中的任何重複記錄,並保留最新記錄(根據日期)。在下面的例子中,第一條記錄將被刪除(hdate = 2012-07-01,id = 16)。刪除表中的重複記錄並保留最新的

使用SQL Server 2008

感謝

hdate  id   secId  pricesource   price   
---------- ------------ ----------- -------------------- -------------- 
2012-07-01 16   126   DFLT     NULL   
2012-07-02 16   126   DFLT     NULL   
2012-07-01 CAD   20   DFLT     1    
2012-07-01 TWD   99   DFLT     1 

回答

0

我ñ如果您的RDBMS不支持熱膨脹係數,或者能夠從他們刪除(因爲你使用的是什麼,你還沒有上市),這裏的其他一切版本:

DELETE FROM TableName as a 
WHERE EXISTS (SELECT '1' 
       FROM TableName b 
       WHERE b.id = a.id -- Plus all other 'duplicate' columns 
        AND b.hdate > a.hdate); 

(和蒂姆的修改Fiddle demo - 雖然由於某些原因,這不適用於SQL Server)。

2

與SQL-Server 2005或更高,您可以使用ROW_NUMBER用適當的OVERCTE

WITH CTE AS 
(
    SELECT hdate, id, secId, pricesource, price, 
    ROW_NUMBER() OVER (PARTITION BY id, secId, pricesource, price ORDER BY hdate DESC) AS RN 
    FROM dbo.TableName t 
) 
DELETE FROM CTE WHERE RN > 1 

Here's a Sql-Fiddle demo

+0

根據海報的例子,處理某些列中可能的空值的好方法將被視爲「相等」 –

0

這不像Tim的解決方案那樣優雅,但不需要CTE。它也處理列中的空值等價物。

DELETE 
FROM MyTable m1 
WHERE EXISTS (
    SELECT 1 
    FROM MyTable m2 
    WHERE 
     (m2.id = m1.id OR (m2.id IS NULL AND m1.id IS NULL)) 
    AND (m2.secId = m1.secId OR (m2.secId IS NULL AND m1.secId IS NULL)) 
    AND (m2.pricesource = m1.pricesource OR (m2.pricesource IS NULL AND m1.pricesource IS NULL)) 
    AND (m2.price = m1.price OR (m2.price IS NULL AND m1.price IS NULL)) 
    AND m2.hdate > m1.hdate 
); 
相關問題