| Author |
Topic |
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-02-02 : 01:13:26
|
| How to remove the duplicates from the table leaving the highest onecol1 col212 456 19 345 45 34612 289 78 67812 900I need to remove the duplicates from col1. here col1 contains duplicate value(12). But instead of removing all the 3 rows, i need to remove all except the highest one(i.e 12-900). i have to remove other 2 rows(12-456.12-289)G. Satish |
|
|
AvanthaSiriwardana
Yak Posting Veteran
78 Posts |
Posted - 2009-02-02 : 01:23:36
|
| SELECT col1, MAX(col2) FROM tableGROUP BY col1Avantha SiriwardanaBeware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth) |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2009-02-02 : 01:25:31
|
| google broken is it?Seriously, I put your very first sentence into google and got a million hits.This site has tons of examples, you really don't have to create a new topic for this. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 01:26:21
|
| DELETE tFROM(SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY col2 DESC) AS SeqFROM Table)tWHERE Seq>1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 01:31:57
|
quote: Originally posted by AvanthaSiriwardana SELECT col1, MAX(col2) FROM tableGROUP BY col1Avantha SiriwardanaBeware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth)
i dont think you read question carefully. question was to delete all except the one with maxvalue. |
 |
|
|
AvanthaSiriwardana
Yak Posting Veteran
78 Posts |
Posted - 2009-02-02 : 01:45:51
|
quote: Originally posted by visakh16
quote: Originally posted by AvanthaSiriwardana SELECT col1, MAX(col2) FROM tableGROUP BY col1Avantha SiriwardanaBeware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth)
i dont think you read question carefully. question was to delete all except the one with maxvalue.
i think the same thing about you.Avantha SiriwardanaBeware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth) |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-02 : 01:54:22
|
| we can write avantha query asDeclare @t table(col1 int,col2 int) --Insert sample dataInsert into @t values(12,456)Insert into @t values(19,345)Insert into @t values(45,346)Insert into @t values(12,289)Insert into @t values(78,678)Insert into @t values(12,900)delete from @t where col2 not in ( SELECT MAX(col2) FROM @tGROUP BY col1 )select * from @t |
 |
|
|
AvanthaSiriwardana
Yak Posting Veteran
78 Posts |
Posted - 2009-02-02 : 01:58:21
|
| thats why i said bklr.Avantha SiriwardanaBeware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth) |
 |
|
|
ra.shinde
Posting Yak Master
103 Posts |
Posted - 2009-02-02 : 02:05:24
|
quote: Originally posted by visakh16 DELETE tFROM(SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY col2 DESC) AS SeqFROM Table)tWHERE Seq>1
This one looks a most perfect solution than others.Rahul Shinde |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 02:08:22
|
quote: Originally posted by ra.shinde
quote: Originally posted by visakh16 DELETE tFROM(SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY col2 DESC) AS SeqFROM Table)tWHERE Seq>1
This one looks a most perfect solution than others.Rahul Shinde
thanks Rahul |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 02:10:29
|
quote: Originally posted by bklr we can write avantha query asDeclare @t table(col1 int,col2 int) --Insert sample dataInsert into @t values(12,456)Insert into @t values(19,345)Insert into @t values(45,346)Insert into @t values(12,289)Insert into @t values(78,678)Insert into @t values(12,900)delete from @t where col2 not in ( SELECT MAX(col2) FROM @tGROUP BY col1 )select * from @t
still not correct boss you may be better off testing this well before suggesting. see belowDeclare @t table(col1 int,col2 int) --Insert sample dataInsert into @t values(12,456)Insert into @t values(19,345)Insert into @t values(45,346)Insert into @t values(12,289)Insert into @t values(78,678)Insert into @t values(12,900)Insert into @t values(13,900)Insert into @t values(13,1200)--bklrdelete from @t where col2 not in ( SELECT MAX(col2) FROM @tGROUP BY col1 )Declare @t1 table(col1 int,col2 int) --Insert sample dataInsert into @t1 values(12,456)Insert into @t1 values(19,345)Insert into @t1 values(45,346)Insert into @t1 values(12,289)Insert into @t1 values(78,678)Insert into @t1 values(12,900)Insert into @t1 values(13,900)Insert into @t1 values(13,1200)--visakhdelete t1 from @t1 t1 where col2 not in ( SELECT MAX(col2) FROM @t1 WHERE col1=t1.col1GROUP BY col1 )select * from @tselect * from @t1output--------------------------------bklr--------------------------------col1 col219 34545 34678 67812 90013 90013 1200visakh---------------------------------col1 col219 34545 34678 67812 90013 1200 |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-02 : 02:23:14
|
k thats fine visakh my code will fails in some cases |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 02:29:25
|
No probs |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 02:35:59
|
quote: Originally posted by AvanthaSiriwardana
quote: Originally posted by visakh16
quote: Originally posted by AvanthaSiriwardana SELECT col1, MAX(col2) FROM tableGROUP BY col1Avantha SiriwardanaBeware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth)
i dont think you read question carefully. question was to delete all except the one with maxvalue.
i think the same thing about you.Avantha SiriwardanaBeware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth)
Hope now you understood what i meant |
 |
|
|
|