У меня есть таблица A:
Create table A(
Name varchar(10),
Number integer,
Exc integer,
D1 date
)
Я вставил 11 строк.
Sel * from A;
+ -----+--------+-----+------------+
| NAME | NUMBER | EXC | D1 |
+ -----+--------+-----+------------+
| a | 1 | 1 | 2020-02-03 |
| a | 1 | 2 | 2020-02-03 |
| a | 1 | 3 | 2020-02-03 |
| a | 1 | 4 | 2020-02-03 |
| a | 1 | 1 | 2020-02-04 |
| a | 1 | 2 | 2020-02-04 |
| a | 1 | 3 | 2020-02-04 |
| a | 1 | 1 | 2020-02-05 |
| a | 1 | 2 | 2020-02-05 |
| a | 1 | 3 | 2020-02-05 |
| a | 1 | 4 | 2020-02-05 |
+ -----+--------+-----+------------+
Теперь, когда я применяю плотный ранг, как показано ниже:
sel vt.*,dense_rank() OVER(PARTITION BY Name,Number,EXC ORDER BY D1 ) AS rn
from vt;
Выход:
+ -----+--------+-----+------------+----+
| NAME | NUMBER | EXC | D1 | RN |
+ -----+--------+-----+------------+----+
| a | 1 | 1 | 2020-02-03 | 1 |
| a | 1 | 2 | 2020-02-03 | 1 |
| a | 1 | 3 | 2020-02-03 | 1 |
| a | 1 | 4 | 2020-02-03 | 1 |
| a | 1 | 1 | 2020-02-04 | 2 |
| a | 1 | 2 | 2020-02-04 | 2 |
| a | 1 | 3 | 2020-02-04 | 2 |
| a | 1 | 1 | 2020-02-05 | 3 |
| a | 1 | 2 | 2020-02-05 | 3 |
| a | 1 | 3 | 2020-02-05 | 3 |
| a | 1 | 4 | 2020-02-05 | 2 |
+ -----+--------+-----+------------+----+
Ожидаемый:
+ -----+--------+-----+------------+----+
| NAME | NUMBER | EXC | D1 | RN |
+ -----+--------+-----+------------+----+
| a | 1 | 1 | 2020-02-03 | 1 |
| a | 1 | 2 | 2020-02-03 | 1 |
| a | 1 | 3 | 2020-02-03 | 1 |
| a | 1 | 4 | 2020-02-03 | 1 |
| a | 1 | 1 | 2020-02-04 | 2 |
| a | 1 | 2 | 2020-02-04 | 2 |
| a | 1 | 3 | 2020-02-04 | 2 |
| a | 1 | 1 | 2020-02-05 | 3 |
| a | 1 | 2 | 2020-02-05 | 3 |
| a | 1 | 3 | 2020-02-05 | 3 |
| a | 1 | 4 | 2020-02-05 | 3 | <-- Difference here
+ -----+--------+-----+------------+----+