问题
(抱歉,有问题,把它分成小块):
我有以下一个列表A
s
-------------------------------------------
(AAB) Some name1 1234 (XY*) Hello world 12
(BB) Just one 123
和查找表L
a1 | a2
-----+-----
XY* | XY
AAB | A2B
我想:
(AAB, XY*)
a2
条目:(A2B, XY)
(1234, 12)
(A2B1234, XY12)
结果将是
s
-------------------------------------------
{A2B1234, XY12}
{BB123}
我尝试了什么
这是我走了多远:
1:
SELECT array_to_string(regexp_matches(s, '\((.*?)\)', 'g'), '') as in_bracket
FROM A;
3:
SELECT array_to_string(regexp_matches(s, '(\d+)', 'g'), '') as numbers
FROM A;
斗争开始了。我怎么能
in_bracket
)替换为动态查找值?数据:
CREATE TABLE A (
s VARCHAR
);
INSERT INTO a VALUES
('(AAB) Some name1 1234 (BB) More text 99 (XY*) Hello world 12'),
('(BB) Just one 123');
CREATE TABLE L (
a1 VARCHAR(4),
a2 VARCHAR(4)
);
INSERT INTO L VALUES
('XY*', 'XY'),
('AAB', 'A2B');
答案 0 :(得分:1)
我来到这种形式:
WITH parts AS (
SELECT
id,
(regexp_matches(s, '\(([^\)]+)\)[^0-9]*([0-9]+)', 'g'))[1] AS search,
(regexp_matches(s, '\(([^\)]+)\)[^0-9]*([0-9]+)', 'g'))[2] AS number
FROM
A
)
SELECT
array_agg(L.a2 || parts.number)
FROM
parts
JOIN L
ON (L.a1 = parts.search)
GROUP BY
parts.id;
输出结果为:
{A2B1,XY12}
{BB123}
有一些事情需要澄清:
A2B1
返回而不是A2B1234
L
中的BB
的条目 - 我已将其手动添加到我的测试L
表中 - 在这种情况下是否应该使用括号中的文字?A
中的结果进行分组,我假设有id
列 - 如果它是quniue,您也可以使用s
列您可能需要调整我使用过的正则表达式。以下是对我的版本的解释:
\( - '('
([^\)]+) - match of sequence of any char except ')' - this will be for example `AAB` or `XY*`
\) - ')'
[^0-9]* - any sequence of chars except digits (no-matching group)
([0-9]+) - match of non-empty sequence of digits