column values vs dense rank vs rank vs row number
suggest changehere you can find the functions.
With the table wf_example created in the previous example, run:
select i
, dense_rank() over (order by i)
, row_number() over ()
, rank() over (order by i)
from wf_example
The result is:
i | dense_rank | row_number | rank
---+------------+------------+------
1 | 1 | 1 | 1
1 | 1 | 2 | 1
1 | 1 | 3 | 1
2 | 2 | 4 | 4
2 | 2 | 5 | 4
3 | 3 | 6 | 6
4 | 4 | 7 | 7
5 | 5 | 8 | 8
row_number() OVER () has no ORDER BY, so the row order is not defined. The table is one possible result.
- dense_rank numbers the distinct values of
i. The first three rows arei=1, sodense_rankstays 1. The fourth row isi=2, the second distinct value, sodense_rankis 2, and the same for the next row. Row 6 isi=3, sodense_rankis 3. The lastdense_rankequals the number of distinct values ofi. - row_number numbers rows in the window order, with no repeated values.
- rank uses the same ordering as
dense_rank, but after a tie the next rank skips ahead. Three rows share rank 1, so the next new value (i=2) is rank 4.i=3starts at row 6, so its rank is 6.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents