0

How to use rank function in MySQL? Below is my requirement.

Question

I need the rank based on FirstName, LastName,MemberID & DOB. If a record matches all the listed four columns then I need to assign rank as above. In oracle I have rank function to obtain the same. How to achieve the same in MySQL?

Cœur
  • 37,241
  • 25
  • 195
  • 267
chaitanya
  • 11
  • 1

1 Answers1

-1

You can do this with variables in SQL Server:

select t.*,
       (@rn := if(@v = concat_ws(':', FirstName, LastName, MemberID, DOB), @rn + 1,
                  if(@v := concat_ws(':', FirstName, LastName, MemberID, DOB), 1, 1)
                 )
       ) as rank  
from t cross join
     (select @rn := 0, @v := '') params
order by FirstName, LastName, MemberID, DOB, lastLoginTime
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786