再向大家请教一个简单的SQL语句(100分)

  • 主题发起人 主题发起人 qdzb
  • 开始时间 开始时间
Q

qdzb

Unregistered / Unconfirmed
GUEST, unregistred user!
有一表T如下,想得到右边的形式
A   B  C               A   B  C
--------            --------- 
T1  1  AA              T1 8  DD
T2  2 BB T2 2  BB
T3  3  CC   -------> T3 3  CC
T1  8  DD
T1  4  EE
即仅取B字段最大的那一行,请问如何实现,谢谢!
 
select * from T where B in (select max(B) B from T group by A)
 
SELECT *
FROM (SELECT MAX(B) AS LB
FROM T
GROUP BY A) B INNER JOIN
T ON B.LB = T.B
 
select * from T where B in (select max(B) B from T group by A) ORDER BY A,B,C
 
Select AA.A,AA.tmp,BB.C
From (Select A,max(B) as tmp From T) AA,T BB
Where AA.A=BB.A and AA.B=BB.B
 
select a.* from T a,(select A,max(B) B from T group by A) b
where a.A = b.A and a.B = b.B
order by a.A, a.B
 
select T.A,T2.maxB B,T.C from T,
(select A,max(B) maxB from T group by A) T2 where T.A=T2.A and T.B=T2.maxB
假设没有重复行,如果有的话可以先消除重复行
select T.A,T2.maxB B,T.C from (select distinct A,B,C from T ) T,(select A,max(B) maxB from T group by A) T2 where T.A=T2.A and T.B=T2.maxB
没有调试,你可以试一下
 
好了,谢谢大家。
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
851
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部