| Author |
Topic |
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-02-24 : 02:42:06
|
| Hi, In my table i have lakhs of records. some of the records are repeating. I want to keep one record in table and remove the repeating ones. I dont know how many duplicates will be in table like this. The table contains lakhs of recordsEx:ZID white black green40 2 3 456 4 4 740 2 3 451 3 4 634 3 3 334 3 3 3G. Satish |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-02-24 : 03:02:28
|
| Here is example with your sample dataDECLARE @T TABLE(ZID int, white int, black int,green int)INSERT INTO @T SELECT 40, 2, 3, 4 UNION ALL SELECT 56, 4, 4, 7 UNION ALL SELECT 40, 2, 3, 4 UNION ALL SELECT 51, 3, 4, 6 UNION ALL SELECT 34, 3, 3, 3 UNION ALL SELECT 34, 3, 3, 3SELECT *FROM @T;WITH Cte AS(SELECT ZID, white, black, green, ROW_NUMBER() Over (PARTITION BY zId ORDER BY ZID) as SeqFROM @T)DELETE FROM cteWHERE Seq > 1SELECT * FROM @TMangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2009-02-24 : 03:22:11
|
| Mangal Pardeshi, i think that you wrongi he have 40 2 2 2so he did'nt want that will delete, and your solution delete duplicate on ZID only. |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-24 : 03:23:48
|
| select distinct * from urtable |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-24 : 03:24:54
|
| then use ROW_NUMBER() Over (PARTITION BY zId,white,black,green ORDER BY ZID) |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-24 : 03:25:16
|
| delete t from(select row_number() over ( partition by zid order by zid)as rn from urtable) twhere t.rn > 1 |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-02-24 : 03:25:44
|
| It depends, I given the solution on sample data provided. If want to check for complete row here you goDECLARE @T TABLE(ZID int, white int, black int,green int)INSERT INTO @T SELECT 40, 2, 3, 4 UNION ALL SELECT 56, 4, 4, 7 UNION ALL SELECT 40, 2, 3, 4 UNION ALL SELECT 51, 3, 4, 6 UNION ALL SELECT 34, 3, 3, 3 UNION ALL SELECT 34, 3, 3, 4 UNION ALL SELECT34, 3, 3, 3SELECT *FROM @T;WITH Cte AS(SELECT ZID, white, black, green, ROW_NUMBER() Over (PARTITION BY ZID, white, black, green ORDER BY ZID) as SeqFROM @T)DELETE FROM cteWHERE Seq > 1SELECT * FROM @TMangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-24 : 03:26:53
|
| Try this alsodeclare @temp table (ZID int, white int,black int, green int)insert into @tempselect 40, 2, 3, 4 union allselect 56,4 ,4, 7 union allselect 40 ,2 ,3, 4 union allselect 51, 3 ,4, 6 union allselect 34, 3, 3, 3 union allselect 34 ,3, 3, 3delete t from(select row_number() over ( partition by zid order by zid)as rn from @temp) twhere t.rn > 1select * from @temp |
 |
|
|
whitmoj
Yak Posting Veteran
68 Posts |
Posted - 2009-02-24 : 09:43:17
|
| This is how I do my dup run select distinct *into dbo.tblRawAccessOffShoreExTWFMC01from dbo.tblRawAccessOffShoreExTWFMCI then truncate the original table and update with the temp tbl then drop temp tableWhitmojIf I have inspired one person today then my job is done. |
 |
|
|
|
|
|