22,300
社区成员




SELECT * FROM tbl T
WHERE NOT EXISTS(SELECT 1 FROM tbl WHERE [user]=T.[user] AND (yw>T.yw or (yw=T.yw and id>t.id)))
order by yw desc,ss desc,id desc
create table table1(id int identity(1,1),usera varchar(20),yw int,ss int,datea smalldatetime)
insert into table1
select 'a',87,90,'2011-2-5' union all
select 'a',45,89, '2011-2-6' union all
select 'b',91,45, '2011-2-7' union all
select 'c',89,69, '2011-2-8' union all
select 'c',89,74, '2011-2-9' union all
select 'b',91,45, '2011-2-10' union all
select 'a',34,12, '2011-2-11' union all
select 'b',24,53, '2011-2-12' union all
select 'c',55,34,'2011-2-13'
select * from table1 a where not exists(select 1 from table1 where a.usera=table1.usera and (table1.yw>a.yw or table1.yw=a.yw and table1.ss>a.ss or table1.yw=a.yw and table1.ss=a.ss and table1.datea>a.datea )) order by usera
/*
id usera yw ss datea
----------- -------------------- ----------- ----------- ----------------------------1 a 87 90 2011-02-05 00:00:00
6 b 91 45 2011-02-10 00:00:00
5 c 89 74 2011-02-09 00:00:00
(所影响的行数为 3 行)
*/
SELECT * FROM tbl T
WHERE NOT EXISTS(SELECT 1 FROM tbl WHERE [user]=T.[user] AND (yw>T.yw or (yw=T.yw and id>t.id)))
order by yw desc,ss desc,id desc