数据库取最大值对应项?
各位大师:请教一下:
ID NAME NUM IDNUM
1 TTTT 01 A010
3 CCC 02 A020
5 DDD 02 A021
6 DD 03 B031
7 EE 03 B032
8 GG 01 C010
9 JJ 02 C021
10 HH 03 C031
如上表所示:我想取出IDNUM相同项(前一字母相同)的NUM最大值所对应的全部项,比如IDNUM的A对应最大值NUM为02,则取出两个02对应的NAME、NUM、IDNUM项值,如何用SQL语句实现哦?!
------解决方案--------------------select * from Table1 as a where not exists(select 1 from Table1 where left(IDNum,1)=left(a.IDNum,1) and Num>a.Num)
------解决方案--------------------select * from table order by idnum having num=max(num)