本文介绍了ROW_NUMBER()
和DENSE_RANK()
之间的一个有趣的关系(RANK()
函数没有具体处理)。当您需要ROW_NUMBER()
在SELECT DISTINCT
语句上生成时,将在被关键字删除之前ROW_NUMBER()
生成不同的值DISTINCT
。例如这个查询
SELECT DISTINCT
v,
ROW_NUMBER() OVER (ORDER BY v) row_number
FROM t
ORDER BY v, row_number
...可能会产生这个结果(DISTINCT
没有效果):
+---+------------+
| V | ROW_NUMBER |
+---+------------+
| a | 1 |
| a | 2 |
| a | 3 |
| b | 4 |
| c | 5 |
| c | 6 |
| d | 7 |
| e | 8 |
+---+------------+
而这个查询:
SELECT DISTINCT
v,
DENSE_RANK() OVER (ORDER BY v) row_number
FROM t
ORDER BY v, row_number
...在这种情况下产生你可能想要的东西:
+---+------------+
| V | ROW_NUMBER |
+---+------------+
| a | 1 |
| b | 2 |
| c | 3 |
| d | 4 |
| e | 5 |
+---+------------+
请注意,ORDER BY
该函数的子句DENSE_RANK()
将需要该子句中的所有其他列SELECT DISTINCT
才能正常工作。
比较所有三个功能
使用 PostgreSQL / Sybase / SQL 标准语法(WINDOW
从句):
SELECT
v,
ROW_NUMBER() OVER (window) row_number,
RANK() OVER (window) rank,
DENSE_RANK() OVER (window) dense_rank
FROM t
WINDOW window AS (ORDER BY v)
ORDER BY v
... 你会得到:
+---+------------+------+------------+
| V | ROW_NUMBER | RANK | DENSE_RANK |
+---+------------+------+------------+
| a | 1 | 1 | 1 |
| a | 2 | 1 | 1 |
| a | 3 | 1 | 1 |
| b | 4 | 4 | 2 |
| c | 5 | 5 | 3 |
| c | 6 | 5 | 3 |
| d | 7 | 7 | 4 |
| e | 8 | 8 | 5 |
+---+------------+------+------------+