写一个检索的SQL文

来源:百度知道 编辑:UC知道 时间:2024/06/05 18:49:19
在一个表T里面,存着这样的数据
列 A1 A2 A3 A4
数据 1 1 a b
1 2 c d
1 3 e f
2 1 g h
2 2 i j

选出相同A1里面A2最大的数据,如下
1 3 e f
2 2 i j

这个SQL该怎么写呀?

select * from
( select max(A2) max_A2,A1 from T group by A1) tmp,T
where max_A2=T.A2 and tmp.A1=T.A1

select a1,max(a2) from t group by a1

楼上的方法,后面的两列取不到。
准确的应该这么写:

select t.* from t,(select a1,max(a2) maxA2 from t group by a1) x
where t.a1=x.a1 and t.a2=x.maxA2