Friday, June 10, 2011

How to realize the ORACLE row_number() over(partition by ) function with MYSQL

When we want to row_number() over(partition by ) in MYSQL, but mysql does not exists this function,how to realize it by SQL?

the sql example as following:

select id,uri,sequence,url_id,rank from (
select tmp.id,tmp.uri,@rownum:=@rownum+1 as rnum,sequence,url_id,
if(@pid=tmp.id,@rank:=@rank+1,@rank:=1) as rank,
@pid:=tmp.id
from (
select a.id,a.uri,b.sequence,b.url_id from urls a, url_measure b
where a.id=b.url_id and b.level=1 order by a.id,exec_time desc
) tmp ,(select @rownum :=0 , @pid :=0 ,@rank:=0) a ) result
where rank=1 limit 0,5




1 comment:

  1. Thanks friend for suggesting a solution to solve this issue with the help of SQL. I will try the above example to see if it really works and fits to my program code or not.
    sap upgrades

    ReplyDelete