简体   繁体   English

PostgreSQL. 如何连接两个字符串值而不重复

[英]PostgreSQL. How to concatenate two strings value without duplicates

I have two strings as below:我有两个字符串如下:

_var_1 text := '815 PAADLEY ROAD PL';
_var_2 text := 'PAADLEY ROAD PL';
_var_3 text;

I want to merge these two strings into one string and to remove duplicates:我想将这两个字符串合并为一个字符串并删除重复项:

_var_3 := _var_1 || _var_2; 

As a result, the variable (_var_3) should contain only - 815 PAADLEY ROAD PL without dublicate.因此,变量 (_var_3) 应仅包含 - 815 PAADLEY ROAD PL ,无需重复。 Can you advise or help recommend any PostgreSQL feature?你能建议或帮助推荐任何 PostgreSQL 功能吗?

I read the documentation and could not find the necessary string function to solve this problem... I am trying to use regexp_split_to_table but nothing is working.我阅读了文档,但找不到必要的字符串 function 来解决这个问题......我正在尝试使用regexp_split_to_table但没有任何效果。

I tried to use this method, but it's not what I need and the words in the output are mixed up::我尝试使用此方法,但这不是我需要的,并且 output 中的单词混淆了::

WITH ts AS (
    SELECT
        unnest(
            string_to_array('815 PAADLEY ROAD PL PAADLEY ROAD PL', ' ')
        ) f
)
SELECT
    f
FROM ts
GROUP BY f

-- f
-- 815
-- ROAD
-- PL
-- PAADLEY

I assume you want to treat strings as word lists and then you have to concat them like they were a sets to be unioned, with retaining order.我假设您想将字符串视为单词列表,然后您必须将它们连接起来,就像它们是要合并的集合一样,并保留顺序。 This is basically done by following SQL:这基本上是通过以下 SQL 完成的:

with splitted (val, input_number, word_number) as (
  select v, 1, i
  from unnest(regexp_split_to_array('815 PAADLEY 2 ROAD 3 PL',' ')) with ordinality as t(v,i)
  union
  select v, 2, i
  from unnest(regexp_split_to_array('PAADLEY ROAD 4 PL',' ')) with ordinality as t(v,i)
), numbered as (
  select val, input_number, word_number, row_number() over (partition by val order by input_number, word_number) as rn
  from splitted
)
select string_agg(val,' ' order by input_number, word_number)
from numbered
where rn = 1 

string_agg string_agg
815 PAADLEY 2 ROAD 3 PL 4 815 帕德利 2 路 3 PL 4

fiddle小提琴

However this is not kind of task to be solved in SQL in smart and elegant way.然而,这不是 SQL 中以智能和优雅的方式解决的任务。 Moreover, it is not clear from your specification what to do with duplicate words or if you want to process multiple input pairs (both requirements would be possible, though SQL is probably not the right tool).此外,从您的规范中不清楚如何处理重复的单词,或者您是否要处理多个输入对(这两种要求都是可能的,尽管 SQL 可能不是正确的工具)。 At least please provide more sample inputs with expected outputs.至少请提供更多具有预期输出的示例输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM