Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-10-28 : 08:00:20
|
Anil Singh writes "I went for interview at satyam. He asked me to write a query to delete the duplicate recordsfrom the table as : ID Name 1 A 2 B 1 A 2 B 2 B 3 C 3 C 4 D I have to write a single query to delete the duplicate records, keeping single copy of each record.Delete from table1......." |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-10-28 : 08:44:07
|
read this:http://www.sqlteam.com/item.asp?ItemID=3331you could also use union...select * from(select * from table1union select * from table1) t1insert that into a temp table. delete the records from previous table and insert non duplicates from temp table into an old table.i don't think you can delete the duplicates with one delete... at least i haven't seen it yet.Go with the flow & have fun! Else fight the flow |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-10-28 : 13:43:58
|
this loops and deletes 1 duplicate at a time...( not the fastest way though )set nocount onset rowcount 0create table #t(ID int, Name varchar)insert #tselect 1,'A' union allselect 2,'B' union allselect 1,'A' union allselect 2,'B' union allselect 2,'B' union allselect 3,'C' union allselect 3,'C' union allselect 4,'D'set rowcount 1; select '' -- The select is just to get @@rowcount = 1while @@rowcount > 0 delete #t from #t join( select ID,Name from #t group by ID,Name having count(*) > 1 ) dups on #t.ID = dups.ID and #t.Name = dups.Nameset rowcount 0select * from #tdrop table #t rockmoose |
|
|
dsdeming
479 Posts |
Posted - 2004-10-29 : 11:12:38
|
Here's another method that I've used.SET NOCOUNT ONCREATE TABLE #temp( ID int, Name char( 1 ))INSERT INTO #temp SELECT 1, 'A'INSERT INTO #temp SELECT 2, 'B'INSERT INTO #temp SELECT 1, 'A'INSERT INTO #temp SELECT 2, 'B'INSERT INTO #temp SELECT 2, 'B'INSERT INTO #temp SELECT 3, 'C'INSERT INTO #temp SELECT 3, 'C'INSERT INTO #temp SELECT 4, 'D'ALTER TABLE #temp ADD RowNumber int IDENTITY( 1, 1 )GODELETE FROM #tempWHERE RowNumber NOT IN( SELECT RowNumber = MIN( RowNumber ) FROM #temp GROUP BY ID, Name )ALTER TABLE #temp DROP COLUMN RowNumberGOSELECT * FROM #tempDROP TABLE #tempDennis |
|
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-10-29 : 12:29:30
|
It's hard to say how badly this will perform, but it satisfies the 'single query' requirement (at least it's a single statement, and the interviewer probably won't know what it does.)master..xp_execresultset 'select ''set rowcount ''+cast(count(1)-1 as varchar)+''; delete from table1 where id = ''+cast(id as varchar) from table1 group by id, name having count(1) > 1',N'mydb'--KenI want to die in my sleep like my grandfather, not screaming in terror like his passengers. |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-10-29 : 12:37:11
|
Nice Kselvia I tried to "conform" to the 1 query requirement,but I didn't go as far as You did *LOL*But doesn't your query purge the table a bit too much ?( for the record - didn't try it )rockmoose |
|
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-10-29 : 12:38:08
|
It shouldn't because it sets rowcount rows - 1 where rows > 1.--KenI want to die in my sleep like my grandfather, not screaming in terror like his passengers. |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-10-29 : 12:59:34
|
You are right Kselvia, didn't read it well enough.nice nicegive that to the interviewer , and I am not sure you will get hired,unless it's SqlTeam that is hiring...rockmoose |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-10-29 : 13:30:26
|
"You're hired because you know how to use SQL Team" ... We need the Snitz Interview-click-through-commission module!Kristen |
|
|
|