2 Votes Vote

ROW_NUMBER() in MySQL

Posted by topdog 265 days ago Questions| row number col All

Is there a nice way in MySQL to replicate the MS SQL Server function ROW_NUMBER()?

For example:

SELECT 
    col1, col2, 
    ROW_NUMBER() OVER (PARTITION BY col1, col2 ORDER BY col3 DESC) AS intRow
FROM Table1

Then I could, for example, add a condition to limit intRow to 1 to get a single row with the highest col3 for each (col1, col2) pair.

Originally asked by: Paul on Stack Overflow

Discuss Bury


Who Voted for this Question