日期:2014-05-18  浏览次数:20502 次

求一SQL语句,MAX有关>>>>>>>>>>>>>>>>>>>在线
Code           ChangeID
A                     1
A                     2
A                     3
B                     1
B                     2
需要结果

A                     3
B                     2
既Code相等时候,取ChangeID最大的行

------解决方案--------------------
select code,max(ChangeID) as ChangeID from tablename group by code
------解决方案--------------------
select code ,max(changeid) from tablename group by code
------解决方案--------------------

----方法1:
select * from 表 as a where not exists(select 1 from 表 where Code=a.Code and ChangeID> a.ChangeID)
----方法2:
select * from 表 as a where ChangeID= (select max(ChangeID) from 表 where Code= a.Code)
order by Code
----方法3:
select a.* from 表 as a inner join (select Code,max(ChangeID) as ChangeID from 表 group by Code) as b
on b.Code= a.Code and a.ChangeID= b.ChangeID order by a.PID

------解决方案--------------------
估計是不止這兩列,參考ljsql(第 1 行: '脑子 ' 附近有语法错误。) 的吧