| Author |
Topic |
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-02-10 : 01:40:23
|
| First of all..sorry friends, Before i posted wrongly the requirement.Below is my requirement. Hope this will be clear now..col1 col2 col3 col4 col5......col70 12 456 33 6 19 345 45 0 45 346 56 91 12 456 33 11 78 678 70 5 12 456 33 3I need to remove the duplicates from above table. Observe the rowswith (12-456-33) is repeating. Now i want to remove 2 rows among the three repetative based on col4. Col4 has a highest value 11 for repetative rows. so by keeping 4th row in table and remove other duplicate rows.G. Satish |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-10 : 01:51:32
|
| select * from ( select * ,row_number() over( partition by col1 order by col2 desc) as rn from tbl1) twhere t.rn = 1 |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-10 : 01:58:04
|
Try this Once,declare @temp table (col1 int,col2 int)insert into @tempselect 12, 456 union allselect 19, 345 union allselect 45, 346 union allselect 12, 289 union allselect 78, 678 union allselect 12, 900delete t from ( select * ,row_number() over( partition by col1 order by col2 desc) as rn from @temp) twhere t.rn > 1select * from @temp |
 |
|
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-02-10 : 01:59:08
|
This query is based on only col1. I need to filter the data based on two columns col1, col2.quote: Originally posted by Nageswar9 select * from ( select * ,row_number() over( partition by col1 order by col2 desc) as rn from tbl1) twhere t.rn = 1
G. Satish |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-10 : 02:00:51
|
| Try the query declare @temp table (col1 int,col2 int)insert into @tempselect 12, 456 union allselect 19, 345 union allselect 45, 346 union allselect 12, 289 union allselect 78, 678 union allselect 12, 900delete t from ( select * ,row_number() over( partition by col1 order by col2 desc) as rn from @temp) twhere t.rn > 1select * from @temp |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-10 : 02:19:00
|
quote: Originally posted by Nageswar9 Try the query declare @temp table (col1 int,col2 int)insert into @tempselect 12, 456 union allselect 19, 345 union allselect 45, 346 union allselect 12, 289 union allselect 78, 678 union allselect 12, 900delete t from ( select * ,row_number() over( partition by col1 order by col2 desc) as rn from @temp) twhere t.rn > 1select * from @temp
actually you dont need * in derived table. you just need rn value only for deletion |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-10 : 02:21:28
|
quote: Originally posted by visakh16
quote: Originally posted by Nageswar9 Try the query declare @temp table (col1 int,col2 int)insert into @tempselect 12, 456 union allselect 19, 345 union allselect 45, 346 union allselect 12, 289 union allselect 78, 678 union allselect 12, 900delete t from ( select * ,row_number() over( partition by col1 order by col2 desc) as rn from @temp) twhere t.rn > 1select * from @temp
actually you dont need * in derived table. you just need rn value only for deletion
OK, Thank u |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-10 : 02:22:03
|
| welcome |
 |
|
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-02-10 : 02:50:23
|
Please check the requirement once. i had posted wrongly before.quote: Originally posted by visakh16 welcome
G. Satish |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-10 : 03:11:49
|
| [code]delete t from ( select row_number() over( partition by col1,col2,col3 order by col4 desc) as rn from @temp) twhere t.rn > 1[/code] |
 |
|
|
|