小编use*_*153的帖子

将 .csv 值匹配为 INT。如果 .csv 中某个组中的一个值与另一组中的一个值匹配,则合并字符串

这里我们有两组数字。问题是我无法弄清楚如何从数字的输入到输出(下面的 DDL 和 DML 以及这里的小提琴)。

current_data
1,2,3
1,4
1,5,7
8,9,10
10,11,15

expected_outcome
1,2,3,4,5,7
8,9,10,11,15
Run Code Online (Sandbox Code Playgroud)

我们只是尝试根据单个数字是否与任何其他组匹配来匹配一组数字。然后合并所有这些组。

例如。

如果我们有:

('1,2,3'),
('1,4'),
('1,5,7')
Run Code Online (Sandbox Code Playgroud)

我们想要:

(1,2,3,4,5,7)
Run Code Online (Sandbox Code Playgroud)

我们将它们合并为 PostgreSQL 中的一行。

或(另一个例子):

('8,9,10'), 
('10,11,15')
Run Code Online (Sandbox Code Playgroud)

所需的输出:

(8,9,10,11,15)
Run Code Online (Sandbox Code Playgroud)

查询将对这些数字进行分组,因为它们的共同点是数字 10。但它不会与(1,2,3,4,5,7)不共享数字的前一行(即)分组。

当我们将这些组放在一张表中时。如果他们在每一组中至少有一个匹配的号码,他们才会分组在一起。

======== DDL 和 DML ============

create table current (current_data text not null);
create table expected_output (expected_outcome text not null);

insert into current (current_data) values ('1,2,3'),('1,4'),('1,5,7'),('8,9,10'), ('10,11,15');
insert into expected_output (expected_outcome) values ('1,2,3,4,5,7'),('8,9,10,11,15');
Run Code Online (Sandbox Code Playgroud)

postgresql array string string-splitting string-searching

5
推荐指数
1
解决办法
94
查看次数