SQL 分组后,获取每组中的最大值对应的数据

 

select gr,num,dt,(select bys from test where gr=b.gr and dt=b.dt) bys
from 
(
	select gr,count(0) num,max(dt) dt from test group by gr
) b

//如果有重复项,可用如下语句(针对Mysql的limit,Oracle 可用 rownum<2)

select gr,num,dt,(select bys from test where gr=b.gr and dt=b.dt limit 1) bys
from 
(
	select gr,count(0) num,max(dt) dt from test group by gr
) b

  

posted on 2015-12-15 12:49  沈恩忍  阅读(16650)  评论(0)    收藏  举报

导航