sql分组求最⼤值
⽅法⼀:(效率最⾼)
select * from test as a
where typeindex = (select peindex)
from test as b
pe = b.type );
⽅法⼆:(效率次之)记住我
select
a.* from test a,
(select type,max(typeindex) typeindex from test group by type) b
pe = b.type peindex = b.typeindex order pe
⽅法三:
select a.* from test a inner join (select type , max(typeindex) typeindex from test group by type) b pe = b.type peindex = b.typeindex order pe
⽅法四:(效率最低)
select * from
(
select *,ROW_NUMBER() OVER(PARTITION BY type ORDER BY typeindex DESC) as num
from test
) t
where t.num = 1
发布评论