Only for MSSQL 2000-2005 by SQL query and no stored procedures
No cursors and temp tables
The table
create table my_table
(
row_id int identity(1,1),
_key varchar(20),
_total decimal(18,2)
)
Insert the data
insert into my_table (_key,_total) Values('qwe',10)
insert into my_table(_key,_total) Values ('qwe',10)
insert into my_table(_key,_total) Values ('asd',10)
insert into my_table(_key,_total) Values('asd',10)
insert into my_table(_key,_total) Values('zxc',10)
insert into my_table(_key,_total) Values('zxc',10)
insert into my_table(_key,_total) Values('qwe',100)
insert into my_table(_key,_total) Values('qwe',100)
insert into my_table(_key,_total) Values('asd',100)
insert into my_table(_key,_total) Values('asd',100)
insert into my_table(_key,_total) Values('zxc',100)
insert into my_table(_key,_total) Values('zxc',100)
insert into my_table(_key,_total) Values('qwe',50)
insert into my_table(_key,_total) Values('qwe',50)
insert into my_table(_key,_total) Values('asd',50)
insert into my_table(_key,_total) Values('asd',50)
insert into my_table(_key,_total) Values('zxc',50)
insert into my_table(_key,_total) Values('zxc',50)
I need to delete duplicates by _key field and leave rows with maximum _total and among them maximum row_id.
I need to get this result set
8 qwe 100.00
10 asd 100.00
12 zxc 100.00