先写出Oracle 以及SQL Server中ROW_NUMBER()

SELECT * FROM 
(SELECT ROW_NUMBER() OVER (PARTITION BY H.ALARMINDEX ORDER BY H.HANDLETIME DESC) N,
H.* FROM M_ALARMHANDLE H) M
WHERE M.N=1

简要可以理解为通过实体表生成一个经过排序和分组的中间表,并且此中间表带自增列(组内自增)例如,

mysql rownum用法 mysql实现rownumber_Oracle

drop table if exists Wmy;
create table Wmy (id int ,GroupId int ,salary decimal(10,2) );
insert into Wmy values
(1,10,5500.00),
(2,10,4500.00),
(3,20,1900.00),
(4,20,4800.00),
(5,40,6500.00),
(6,40,14500.00),
(7,40,44500.00),
(8,50,6500.00),
(9,50,7500.00);

select id,GroupId,salary,rank from 
(
 select H.id,H.GroupId,H.salary,@rownum:=@rownum+1 ,
 if(@pdept=H.GroupId,@rank:=@rank+1,@rank:=1) as rank,
 @pdept:=H.GroupId
 from (
 select id,GroupId,salary from Wmy order by GroupId asc ,salary desc
 ) H ,(select @rownum :=0 , @pdept := null ,@rank:=0) a ) result

以上是MySQL实现ROW_NUMBER(),