我有一张表格:
id | emp_id | telecom_id |
----+------------+------------------+
1 | 1 | 1 |
2 | 1 | 1 |
3 | 1 | 1 |
4 | 1 | 2 |
5 | 1 | 3 |
6 | 1 | 3 |
7 | 1 | 1 |
8 | 2 | 5 |
9 | 2 | 1 |
10 | 1 | 1 |
11 | 2 | 1 |
12 | 2 | 1 |
以下是创建和填充表格的命令,以方便操作:
CREATE TABLE table1 (
id int NOT NULL,
emp_id varchar(255),
telecom_id varchar(255)
);
insert into table1 (id, emp_id, telecom_id) values(1, '1', '1');
insert into table1 (id, emp_id, telecom_id) values(2, '1', '1');
insert into table1 (id, emp_id, telecom_id) values(3, '1', '1');
insert into table1 (id, emp_id, telecom_id) values(4, '1', '2');
insert into table1 (id, emp_id, telecom_id) values(5, '1', '3');
insert into table1 (id, emp_id, telecom_id) values(6, '1', '3');
insert into table1 (id, emp_id, telecom_id) values(7, '1', '1');
insert into table1 (id, emp_id, telecom_id) values(8, '2', '5');
insert into table1 (id, emp_id, telecom_id) values(9, '2', '1');
insert into table1 (id, emp_id, telecom_id) values(10, '1', '1');
insert into table1 (id, emp_id, telecom_id) values(11, '2', '1');
insert into table1 (id, emp_id, telecom_id) values(12, '2', '1');
我需要对这个表中的行进行排名,使得每个会话的行都有相同的排名。 会话是具有相等emp_id和telecom_id的连续行系列。
例如,行1-3形成一个会话,因为所有3行的emp_id = 1和telecom_id = 1。第4行形成另一个会话。行5-6形成第三个会话等等。
在排名中使用存储在表中的数据顺序非常重要。
期望的输出:
id | emp_id | telecom_id | rnk
----+------------+------------------+------
1 | 1 | 1 | 1
2 | 1 | 1 | 1
3 | 1 | 1 | 1
4 | 1 | 2 | 2
5 | 1 | 3 | 3
6 | 1 | 3 | 3
7 | 1 | 1 | 4
8 | 2 | 5 | 5
9 | 2 | 1 | 6
10 | 1 | 1 | 7
11 | 2 | 1 | 8
12 | 2 | 1 | 8
我尝试了多种窗口函数选项,但没有一种能按预期工作。这是我尝试产生最接近期望结果的方法:
select emp_id, telecom_id, rank()
over(partition by emp_id, telecom_id order by id) as rnk
from table1;
我正在使用PostgreSQL。