SQL获取分组后取某字段最大一条记录(求每个类别中最大的值的列表)

方法一(效率最高):

select * from test as a 
where typeIndex = (select max(b.typeIndex) from test b where a.type = b.type );

方法二(效率次之):

select a.* from test a,
(select type,max(typeindex) typeindex from test group by type) b
where a.type=b.type and a.typeindex = b.typeindex order by a.type

方法三(效率最低):

select * from 
(
select *,ROW_NUMBER() OVER(PARTITION BY type ORDER BY typeindex DESC) AS num
from test
)t where t.num=1
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容