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 |
musawarman
Starting Member
1 Post |
Posted - 2007-03-28 : 04:19:27
|
Hi friends,can any body please tell me how can i delete duplicate records from a table . If the table I have is with the following datafield1 field2 field3 field4 field5----------------------------------------a a a b c a a a b c ------>deleteda a a b d a a a f d ------>deletedfinally, the table will look a like:field1 field2 field3 field4 field5----------------------------------------a a a b c a a a b d Thanx& RegardsMusawarman |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-28 : 04:24:05
|
[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=60443&SearchTerms=delete,duplicate[/url]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-28 : 04:29:33
|
[code]-- Prepare sample dataDECLARE @Sample TABLE (field1 VARCHAR, field2 VARCHAR, field3 VARCHAR, field4 VARCHAR, field5 VARCHAR)INSERT @SampleSELECT 'a', 'a', 'a', 'b', 'c' UNION ALLSELECT 'a', 'a', 'a', 'b', 'c' UNION ALLSELECT 'a', 'a', 'a', 'b', 'd' UNION ALLSELECT 'a', 'a', 'a', 'b', 'd'SET ROWCOUNT 1DECLARE @Dummy INTSELECT @Dummy = 1WHILE @@ROWCOUNT > 0 DELETE s1 FROM @Sample AS s1 WHERE EXISTS ( SELECT * FROM @Sample AS s2 WHERE s1.field1 = s2.field1 AND s1.field2 = s2.field2 AND s1.field3 = s2.field3 AND s1.field4 = s2.field4 AND s1.field5 = s2.field5 GROUP BY s2.field1, s2.field2, s2.field3, s2.field4, s2.field5 HAVING COUNT(*) > 1 )SET ROWCOUNT 0SELECT field1, field2, field3, field4, field5FROM @Sample[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|