Dmi*_*kiy 3 sql postgresql window-functions
我有一张桌子:
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 |
Run Code Online (Sandbox Code Playgroud)
为了方便起见,以下是用于创建和填充表的命令:
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');
Run Code Online (Sandbox Code Playgroud)
我需要以这种方式对表中的行进行排名,即每个会话行都具有相同的排名。会话是一系列具有相同 emp_id 和 Telecom_id 的连续行。
例如,第 1-3 行形成一个会话,因为emp_id = 1并且telecom_id = 1对于所有 3 行。第 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
Run Code Online (Sandbox Code Playgroud)
我尝试了窗口函数的各种选项,但没有一个按预期方式工作。这是产生与我想要实现的结果最接近的结果的尝试:
select emp_id, telecom_id, rank()
over(partition by emp_id, telecom_id order by id) as rnk
from table1;
Run Code Online (Sandbox Code Playgroud)
我正在使用 PostgreSQL。
您可以尝试使用lag窗口函数 get pre-Val 并使用条件聚合函数SUM和窗口函数来制作逻辑。
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');
Run Code Online (Sandbox Code Playgroud)
查询1:
SELECT id,emp_id,telecom_id,
SUM(CASE WHEN
pretelecomVal = telecom_id
and pre_emp_idVal = emp_id
then 0 else 1 end) over(order by id) rnk
FROM (
select *,
lag(telecom_id) over(partition by emp_id order by id) pretelecomVal,
lag(emp_id) over(order by id) pre_emp_idVal
from table1
) t1
Run Code Online (Sandbox Code Playgroud)
结果:
| 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 |
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1183 次 |
| 最近记录: |