--建表
create table CY_T(Name varchar(10),ID int)
go
insert into CY_T
select 'a', 1
union all select 'a', 2
union all select 'b', 1
union all select 'b', 2
union all select 'b', 3
union all select 'c', 1
union all select 'c', 2
union all select 'c', 3
union all select 'c', 4
--测试
select * from CY_T aa
where ID in (select top 2 ID from CY_T where Name=aa.Name order by ID desc)
order by aa.Name
--结果
/*
----------
Name ID
a 1
a 2
b 2
b 3
c 3
c 4
*/
insert into pp
select 'a', 1 union
select 'a', 2 union
select 'b', 1 union
select 'b', 2 union
select 'b', 3 union
select 'c', 1 union
select 'c', 2 union
select 'c', 3 union
select 'c', 4
----测试
select * from pp a where a.id in (select top 2 id from pp where name=a.name
order by id desc) order by name
----结果
name id
---- --
a 1
a 2
b 2
b 3
c 3
c 4