Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
vicpal25
Starting Member
21 Posts |
Posted - 2008-01-30 : 11:55:33
|
| I have a table called Engr_BOM_Detail and my data is all screwed up. There are duplicate records coming from other tables. I want to delete these records and the only way to do so would be to target those records by the Engr_BOM_Detail's primary key field which is pEngr_BOM_Detail_ID. I have written the SQL that finds the duplicates on my table now, how can I take those records, identify them by their primary key and delete all and leaving only one entity of each record in my table. I was thinking of dropping these into a temp table but the primary key allocation is going to be the trick.SELECT fEngr_Item_ID as "Item ID", COUNT(fEngr_Item_ID) AS NumOccurrencesFROM Engr_BOM_DetailGROUP BY fEngr_Item_ID, fEngr_BOM_IDHAVING COUNT(fEngr_Item_ID) > 1 Item ID NumOccurrences--------------------------------------- --------------5349 2if we go to the actual table using:select pEngr_BOM_Detail_ID, fEngr_BOM_ID, fEngr_Item_ID from Engr_BOM_Detail where fEngr_Item_id=5349pEngr_BOM_Detail_ID fEngr_BOM_ID Engr_Item_ID------------------------- ---------------- -----------------7084 1278 53497085 1279 53497086 1279 53497087 1278 53497088 1255 53497089 1246 5349suggestions? |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-30 : 12:26:42
|
I would suggest thoroughly testing this code against your table and making a copy of your table in its current state before running the code.IF OBJECT_ID('tempdb..#Engr_BOM_Detail') IS NOT NULL DROP TABLE #Engr_BOM_DetailCREATE TABLE #Engr_BOM_Detail (pEngr_BOM_Detail_ID INT PRIMARY KEY, fEngr_BOM_ID INT, Engr_Item_ID INT)INSERT #Engr_BOM_Detail (pEngr_BOM_Detail_ID, fEngr_BOM_ID, Engr_Item_ID)SELECT 7084, 1278, 5349 UNIONSELECT 7085, 1279, 5349 UNIONSELECT 7086, 1279, 5349 UNIONSELECT 7087, 1278, 5349 UNIONSELECT 7088, 1255, 5349 UNIONSELECT 7089, 1246, 5349SELECT * FROM #Engr_BOM_DetailSELECT pEngr_BOM_Detail_ID, fEngr_BOM_ID, Engr_Item_IDINTO #TempTableFROM ( SELECT ROW_NUMBER() OVER ( PARTITION BY Engr_Item_ID, fEngr_BOM_ID ORDER BY pEngr_BOM_Detail_ID ) AS RowNumber, pEngr_BOM_Detail_ID, fEngr_BOM_ID, Engr_Item_ID FROM #Engr_BOM_Detail ) aWHERE RowNumber = 1TRUNCATE TABLE #Engr_BOM_DetailINSERT #Engr_BOM_Detail SELECT * FROM #TempTableDROP TABLE #TempTableSELECT * FROM #Engr_BOM_Detail |
 |
|
|
dnf999
Constraint Violating Yak Guru
253 Posts |
Posted - 2008-01-30 : 12:33:31
|
| Hi VicpalWhen dealing with dupes, this is the usual approach I go for:--Create Unique ID ALTER TABLE Engr_BOM_DetailADD SRN numeric identity (1,1)--Select Max(Unique_ID) for each pEngr_BOM_Detail_IDSELECT a.* FROM Engr_BOM_Detail aINNER JOIN( SELECT pEngr_BOM_Detail_ID, MAX(SRN) as SRN FROM Engr_BOM_Detail GROUP BY pEngr_BOM_Detail_ID) bon a.SRN = b.SRNThis should return only the distinct rows.So basically I add a unique id field for every value in the table and then group by on the dupe field i want and select the maximum SRN associated with that dupe field.Hope this can help. |
 |
|
|
vicpal25
Starting Member
21 Posts |
Posted - 2008-01-30 : 14:00:28
|
| Forgot to mention i am using sql server 2000 so row_number will not work. ANy replacements? Also, this line :SELECT 7084, 1278, 5349 UNIONSELECT 7085, 1279, 5349 UNIONSELECT 7086, 1279, 5349 UNIONSELECT 7087, 1278, 5349 UNIONSELECT 7088, 1255, 5349 UNIONSELECT 7089, 1246, 5349its hard coded there are 1200 records with multiple values in my table |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-30 : 16:22:11
|
quote: Originally posted by vicpal25 Forgot to mention i am using sql server 2000 so row_number will not work. ANy replacements? Also, this line :SELECT 7084, 1278, 5349 UNIONSELECT 7085, 1279, 5349 UNIONSELECT 7086, 1279, 5349 UNIONSELECT 7087, 1278, 5349 UNIONSELECT 7088, 1255, 5349 UNIONSELECT 7089, 1246, 5349its hard coded there are 1200 records with multiple values in my table
You should be able to replace ROW_NUMBER() with RANK() in sql server 2000 and get the same functionality.As far as the initial INSERT into #Engr_BOM_Detail statement I am only doing that to build a representation of your actual table. Replace #Engr_BOM_Detail with your table Engr_BOM_Detail and disregard the first insert statement. |
 |
|
|
|
|
|
|
|