【发布时间】:2018-11-01 10:00:50
【问题描述】:
我有一张桌子:
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 行形成一个会话,因为 emp_id = 1 和 telecom_id = 1 用于所有 3 行。第 4 行形成另一个会话。第 5-6 行形成第 3 次会议等。
在排序时使用数据在表中存储的顺序至关重要。
期望的输出:
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。
【问题讨论】:
-
使用dense_rank而不是rank
-
@AjanBalakumaran,RANK() 和 DENSE_RANK() 产生的结果与我想要达到的结果不同
-
您使用的是哪个 dbms?
-
你能为你的数据集提供一个小提琴或一个声明的表,然后人们可以轻松地为你工作
-
@AjanBalakumaran,更新了我的问题,添加了创建表的命令
标签: sql postgresql window-functions