2010-09-22 81 views
2

下面的代碼說明了我正在努力完成的最好的事情。我知道我可以使用遊標或其他循環例程來遍歷記錄來查找重複項,並根據找到的內容創建筆記記錄。我試圖避免這種情況,除非沒有更好的選擇。數據重複數據刪除

DROP TABLE #orig 
DROP TABLE #parts 
DROP TABLE #part_notes 

CREATE TABLE #orig(partnum VARCHAR(20), notes VARCHAR(100)); 
INSERT INTO #orig VALUES ('A123', 'To be used on Hyster models only') 
INSERT INTO #orig VALUES ('A123', 'Right Hand model only') 
INSERT INTO #orig VALUES ('A125', 'Not to be used by Jerry') 
INSERT INTO #orig VALUES ('A125', NULL) 
INSERT INTO #orig VALUES ('A125', 'asdfasdlfj;lsdf') 
INSERT INTO #orig VALUES ('A128', 'David test') 
INSERT INTO #orig VALUES ('A129', 'Fake part') 

SELECT COUNT(*) FROM #orig 

-- SHOW ME UNIQUE PARTS, MY PARTS TABLE SHOULD BE UNIQUE! 
SELECT DISTINCT partnum FROM #orig 


CREATE TABLE #parts(id INT IDENTITY(1,1), partnum VARCHAR(20)); 
INSERT INTO #parts 
SELECT DISTINCT partnum FROM #orig 

SELECT * FROM #parts 

CREATE TABLE #part_notes(id INT IDENTITY(1,1), part_id INT, line_number INT, notes VARCHAR(100)); 
/* 
    HOW DO I AT THIS POINT POPULATE the #part_notes table so that it looks like this: 
    (note: any NULL or empty note strings should be ignored) 

    id part_id line_number notes 
    1 1  1   To be used on Hyster models only  
    2 1  2   Right Hand model only 
    3 2  1   Not to be used by Jerry 
    4 2  2   asdfasdlfj;lsdf 
    6 3  1   David test 
    7 4  1   Fake part 

*/ 
+0

'line_number'應該如何歸因?如果沒有'ORDER BY',來自'orig'的行將不按特定順序讀取。 – eumiro 2010-09-22 12:25:44

回答

1

以下只是隨意選擇line_numbers,因爲似乎沒有在數據適合order by什麼。

SELECT p.id part_id, 
     p.partnum , 
     ROW_NUMBER() over (partition BY p.id ORDER BY (SELECT 0)) line_number, 
     notes 
FROM  #parts p 
     JOIN #orig o 
     ON  o.partnum=p.partnum 
WHERE notes IS NOT NULL 
AND  notes   <> '' 
ORDER BY part_id 
+0

這對我來說很完美。由於目前筆記沒有訂單,所以我不關心它們添加的順序。謝謝! – dtaylo04 2010-09-22 20:06:15

+0

馬丁 - 有沒有辦法第二次運行這個腳本,並有line_number開始在當前line_number + 1該part_id? – dtaylo04 2010-09-22 21:03:55

+0

回答了我自己的問題: 'ROW_NUMBER()over(分區BY p.id ORDER BY(SELECT 0))+ ISNULL(MAX(n.line_number),0)line_number' 幷包含左外連接: ' LEFT OUTER JOIN #part_notes n on p.id = n.part_id' 您還需要添加「Group By」子句。 – dtaylo04 2010-09-22 21:21:59