同意楼上的 select top 1 B from tablename where 条件1 and 条件2 order by A desc
可以用 order by a desc
我得这个表还有一个分组字段 A ID NAME 1 1 aaa 3 2 aaa 2 3 aaa 5 1 bbb 1 2 bbb 6 3 bbb我想得到 ID NAME 2 aaa 3 bbb 也就是想得到每一个不通得NAME,的max(A) 对应的ID值
--测试数据 if object_id('ta') is not null drop table ta go create table ta( a int, id int, name varchar(10)) insert ta select 1, 1, 'aaa' union all select 3, 2, 'aaa' union all select 2, 3, 'aaa' union all select 5, 1, 'bbb' union all select 1, 2, 'bbb' union all select 6, 3, 'bbb' --查询 select ta.id, ta.name from ta,(select max(a) as a , name from ta group by name) t where ta.a=t.a and ta.name=t.name
select top 1 B from tablename where 条件1 and 条件2 order by A desc
A ID NAME
1 1 aaa
3 2 aaa
2 3 aaa
5 1 bbb
1 2 bbb
6 3 bbb我想得到
ID NAME
2 aaa
3 bbb
也就是想得到每一个不通得NAME,的max(A) 对应的ID值
if object_id('ta') is not null drop table ta
go
create table ta( a int, id int, name varchar(10))
insert ta select 1, 1, 'aaa'
union all select 3, 2, 'aaa'
union all select 2, 3, 'aaa'
union all select 5, 1, 'bbb'
union all select 1, 2, 'bbb'
union all select 6, 3, 'bbb'
--查询
select ta.id, ta.name
from ta,(select max(a) as a , name
from ta
group by name) t
where ta.a=t.a and ta.name=t.name