After obtaining a packet to take a field recording the maximum (the maximum list of requirements in each category value)

After obtaining a maximum field for a packet to take a recording
method of a most efficient :()

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

 


Method Two :( efficiency followed)

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 

 


Method three:

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

 


Method four :( least efficient)

select * from
(
select *,ROW_NUMBER() OVER(PARTITION BY type ORDER BY typeindex DESC) as num
from test
) t
where t.num = 1

Guess you like

Origin www.cnblogs.com/Steven5007/p/12058416.html