| Author |
Topic |
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-04-02 : 01:40:59
|
| hi my table contains data like1 a 1 a 1 a 1 a 1 a 2 b 2 b 3 c 3 c 3 c 3 c i want to delete duplicate and my look like1 a 2 b 3 c RegardsSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceled |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-04-02 : 02:12:18
|
| with ashish as(select * ,row_number() over(partition by data order by id) as rn from ashish4)delete from ashish where rn>1select * from ashish4May Be like This.... |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2009-04-02 : 02:26:45
|
| Try this one & this isnt dependent on the version of Sqlserver that you have.Delete from DupsTablewhere UniqueColumn in (select a.UniqueColumn from DupsTable a, DupsTable bwhere a.UniqueColumn != b.UniqueColumnand a.DuplicateColumn = b.DuplicateColumnand a.UniqueColumn < b.UniqueColumn)regards,Anil Kumar. |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-04-02 : 02:35:57
|
| Hi Anil..Unfortunately i did't have any unique column.RegardsSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceled |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2009-04-02 : 02:51:15
|
| Sure then try this one & let me know if you run into any errors as this might work only on 2005 & beyond.DELETE NFROM (SELECT ROW_NUMBER() OVER (PARTITION BY Col1, Col2 ORDER BY Col1) AS UniqueIdFROM Dtable) AS NWHERE UniqueId > 1regards,Anil Kumar. |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-04-02 : 02:57:47
|
| I'm using SQL SERVER 2000.. The row_number() function is not available here!!What to do??RegardsSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceled |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2009-04-02 : 03:04:46
|
| You can do it using a simple staging table. Just move all of the records into a Temp_Unique table and then drop the table itself. Can you do that ? |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-04-02 : 03:13:03
|
| This May Gonna work......select * into aa from ashish4 group by id,datatruncate table ashish4insert into ashish4 select * from aaselect * from ashish4May Be Like This... |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-04-02 : 03:16:45
|
| Ya thanks Both of u..Thats my last option..But i try something in a query.its Challenging me..Thanks for ur help..RegardsSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceled |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-04-02 : 03:19:38
|
| Try this Once.drop table #tempdeclare @t table (A int ,b varchar(32))insert into @t select 1 , 'a' union all select 1, 'a' union all select 1, 'a' union all select 1, 'a' union all select 1, 'a' union all select 1, 'a' union all select 2, 'b' union all select 2 ,'b' union all select3 ,'c' union all select3 ,'c' union all select3 ,'c' union all select3 ,'c'select identity(int,1,1)as rid, * into #temp from @tdelete t1 from (select t.rid,a,b,(select count(a) from #temp where rid <= t.rid and a= t.a)as rowfrom #temp t )t1 where t1.row > 1select a,b from #temp |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2009-04-02 : 03:25:21
|
| I just gave a thought & if you insist on going that way then this will do it.DELETE DFROM DupsTable DJOIN (SELECT col1, col2 FROM DupsTable GROUP BY col1, Col2 HAVING COUNT(DISTINCT(*)>1))D1ON D1.col1=D.col1AND D1.col2=D.col2WHERE D1.col1 IS NULL |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-04-02 : 03:46:40
|
| Hi Anil,If i match with the Distinct table as ur ref..I will got duplicate entries in my main table na?How it will work?select d.id,d.nameFROM testtable DJOIN(SELECT id, name FROM testtable GROUP BY id, name HAVING COUNT(DISTINCT(id))=1)D1ON D1.id=D.idAND D1.name=D.namewait i will try with left join..RegardsSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceled |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-04-02 : 03:50:42
|
| Hey,In left too the same result..select d.id,d.name from (SELECT id, name FROM testtable GROUP BY id, name HAVING COUNT(DISTINCT(id))=1) D left outer join testtable d1 on D1.id=D.id and d1.name=d.nameRegardsSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceled |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2009-04-02 : 03:52:19
|
| Hey, What I wrote in here & what you wrote are totally different... join & inner join will fetch you the same record set....anyways, Good luck Pal.The whole idea in there is ...Keep the inner set of records & purge the outer one.regards,Anil Kumar. |
 |
|
|
|