| Author |
Topic |
|
rd_innovatives
Starting Member
18 Posts |
Posted - 2006-10-30 : 08:01:25
|
| Hi All,Greetings ..... I am wondering if you can answer me this question ...suppose ... a table contains rows and and many rows are duplicate ..I want to store only the UNIQUE rows in the table ...I dont want to use any temp. table , cursor etccc for that activity ...Can you please help me ....Thanks in Advance :) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 08:04:33
|
For this homework, I writedelete mtfrom mytable mtinner join ( select mycolumn, from mytable group by mycolumn having count(*) > 1 ) q on q.mycolumn = mt.mycolumn Peter LarssonHelsingborg, Sweden |
 |
|
|
rd_innovatives
Starting Member
18 Posts |
Posted - 2006-10-30 : 08:05:55
|
| Thanks peter .. will try this workout :) |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-10-30 : 08:57:58
|
| Umh...wouldn't this delete all the duplicates and leave no rows behind?--Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 09:06:05
|
| not the rows where count(*) = 1Peter LarssonHelsingborg, Sweden |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2006-10-30 : 10:09:06
|
It will kill ALL the dupe rows and not leave one. I would think that you would want to leave ONE of the dupes.create table mytable (foo int identity(1,1),bar varchar(10))insert into mytable(bar)select 'One' union allselect 'One' union allselect 'One' union allselect 'Two' union allselect 'Three' union allselect 'Three'select * from mytable delete mtfrom mytable mtinner join ( select bar from mytable group by bar having count(*) > 1 ) q on q.bar = mt.barselect * from mytable Results: Foo Bar 4 TwoI would expect the results: Foo Bar 1 One 4 Two 5 ThreeEDIT: my preview isnt working. sorry about the formatting[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2006-10-30 : 10:16:49
|
Ok here is a quick and dirty one. Perhaps our resident saint can clean it upcreate table mytable (foo int identity(1,1),bar varchar(10))insert into mytable(bar)select 'One' union allselect 'One' union allselect 'One' union allselect 'Two' union allselect 'Three' union allselect 'Three'select * from mytable WHILE EXISTS( SELECT MAX(Foo) AS Foo,Bar FROM mytable GROUP by Bar HAVING COUNT(*) > 1 )BEGIN DELETE FROM mytable WHERE Foo IN (SELECT Foo FROM (SELECT MAX(Foo) AS Foo,Bar FROM mytable GROUP BY Bar HAVING COUNT(*) > 1 )q )ENDselect * from mytabledrop table mytable [Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-10-30 : 11:20:05
|
oh hell... select col1, col2, coln from(select col1, col2, coln from myTableunion select col1, col2, coln from myTable) t1this will select only one of the dupes. it won't remove them.if you want to remove the duplicates then do:select col1, col2, coln into #tempfrom(select col1, col2, coln from myTableunion select col1, col2, coln from myTable) t1truncate table myTableisnert into myTable(col1, col2, coln )select col1, col2, colnfrom #tempGo with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 11:22:07
|
Even if OP writes followingquote: I want to store only the UNIQUE rows in the table ...
Wouldn't he have writtenquote: I want to store only the DISTINCT rows in the table
if following Lumbago and DonAtWork minds?Peter LarssonHelsingborg, Sweden |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2006-10-30 : 11:26:31
|
I suppose you can read it that way. However, I am used to dealing with USERS at work. I translate what they ask for into something that makes sense. You took him literally. I would have to say you gave him what he asked for. Now, pretty please compact my code into something nice. [Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-10-30 : 11:27:45
|
that could swing both ways depending on the OP's knowledge of the language to the lack of proper techical expresions.Go with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 11:29:57
|
| I am too old and too tired to play games. If OP doesn't care to provide more detailed information, he gets what he asks for.After all, this forum is free of charge.Peter LarssonHelsingborg, Sweden |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-10-30 : 11:32:13
|
hehe just 15 posts to go before you start seeing the light, peter.   Go with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 11:34:47
|
38 more posts for you, old man Peter LarssonHelsingborg, Sweden |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-10-30 : 11:46:11
|
if i'm old then you must be ancient Go with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
|