ROW_NUMBER
Overview
The ROW_NUMBER()
window function returns the number of the current row within its partition (counting from 1), based on the ORDER BY
expression in the OVER
clause. It can be used with all data types supported by Oxla.
Syntax
The syntax for this function is as follows:
The output’s type for this function is BIGINT
. Rows with equal values for the ORDER BY
expression receive different row numbers nondeterministically.
Parameters
()
: this function takes no arguments but parentheses is required
Examples
For the needs of this section, we will create the winsales
table that stores details about some sales transactions:
ROW_NUMBER() with ORDER BY
In this example, we will focus on executing the ROW_NUMBER()
function with ORDER BY
keyword, assign a row number to each row and order the table by the row number (the results will be sorted after the window function results are applied):
Here is the output for the code above:
ROW_NUMBER() with ORDER BY and PARTITION BY
In this example, we will focus on executing the ROW_NUMBER()
function with ORDER BY
keyword and PARTITION BY
clause, partition the table by seller ID, assign a row number to each row and order the table by the sales ID and row number (the results will be sorted after the window function results are applied):
The output of the code above will be as follows: