In PostgreSQL, we can use the percent_rank()
function to return the relative rank of each row, expressed as a percentage ranging from 0 to 1 inclusive.
Tag: what is
Using the NTILE() Function to Divide a Partition into Buckets in PostgreSQL
In PostgreSQL, the ntile()
function is a window function that divides a partition into the specified number of groups (buckets), distributing the rows as equally as possible, and returns the bucket number of the current row within its partition.
Understanding the DENSE_RANK() Function in PostgreSQL
PostgreSQL has a window function called dense_rank()
that returns the rank of the current row, without gaps.
It works the same way that the rank()
function works, but without gaps (the rank()
function includes gaps).
Using the RANK() Function to Add a “Rank” Column in PostgreSQL
PostgreSQL has a window function called rank()
that returns the rank of the current row, with gaps.
“With gaps” means that it returns the same rank for any ties (i.e. two or more rows with the same value), but then subsequent ranks jump forward to account for the ties.
This means that there’s the potential for noncontiguous rank values. For example it could go 1, 2, 5, etc if several rows are ranked at 2. If there are no ties, then the rank values will be contiguous.
Continue readingAdd a Column of Row Numbers in PostgreSQL: The ROW_NUMBER() Function
In PostgreSQL, we can use the row_number()
function to get each row’s number within its partition. This allows us to create a column with incrementing row numbers that reset with each new partition.
The row_number()
function is a window function that’s specifically designed to return the number of the current row within its partition, starting at 1 and incrementing sequentially.
Overview of the WINDOW Clause in SQL
The WINDOW
clause is an optional clause that we can use in our SQL queries to create a named window. The named window can then be used as part of a window function.
When creating a window function, a SQL developer will often define it directly in the OVER
clause. But that’s not the only way to do it. We can alternatively use the WINDOW
clause to define it in a named window, and then refer to that named window in the OVER
clause.
Overview of the ROW_NUMBER() Function in MySQL
In MySQL, ROW_NUMBER()
is a window function that returns the number of the current row within its partition. Numbering starts at 1 and increments sequentially.
Divide a Partition into Buckets with the NTILE() Function in MySQL
In MySQL, the NTILE()
function is a window function that divides a partition into a given number of groups (buckets) and returns the bucket number of the current row within its partition.
Overview of the LEAD() Function in MySQL
In MySQL, the LEAD()
function is a window function that returns the value of a given expression from the row that leads (follows) the current row by a given number of rows within its partition.
Basically, it returns the value from a later row.
Continue readingOverview of the LAG() Function in MySQL
In MySQL, the LAG()
function is a window function that returns the value of a given expression from the row that lags (precedes) the current row by a given number of rows within its partition.
Basically, it returns the value from a previous row.
Continue reading