row_number() rank() dense_rank()区别
时间: 2024-03-15 10:28:42 浏览: 61
ROW_NUMBER(),RANK()和DENSE_RANK()之间的区别
`row_number()`, `rank()`, and `dense_rank()` are window functions in SQL that generate ranking values based on the values in a specified column.
The main differences between these functions are:
1. `row_number()`: This function assigns a unique rank to each row within the partition. The rank is based on the order specified by the `ORDER BY` clause. If two rows have the same values, they will be assigned the same rank.
2. `rank()`: This function assigns a rank to each row within the partition based on the values in the ORDER BY clause. If two or more rows have the same value, they will be assigned the same rank, and the next rank will be skipped.
3. `dense_rank()`: This function assigns a rank to each row within the partition based on the values in the ORDER BY clause. If two or more rows have the same value, they will be assigned the same rank, and the next rank will not be skipped.
In summary, `row_number()` assigns a unique rank to each row, while `rank()` and `dense_rank()` assign ranks based on the values in the ORDER BY clause, and `dense_rank()` assigns consecutive ranks to rows with the same values.
阅读全文