gpt4 book ai didi

sql - 从 Oracle 中的逗号分隔变量中删除重复值

转载 作者:行者123 更新时间:2023-12-04 15:56:40 25 4
gpt4 key购买 nike

我有一个变量(称为:all_email_list),它总共包含 3 个电子邮件地址列表。 (我发现了一些类似的问题,但与正确的解决方案不同)

示例:test@asd.com, test2@asd.com,test@asd.com,test3@asd.com, test4@asd.com,test2@asd.com(可以逗号之间包含空格,但不是一直)

所需的输出:test@asd.com, test2@asd.com,test3@asd.com,test4@asd.com

declare
first_email_list varchar2(4000);
second_email_list varchar2(4000);
third_email_list varchar2(4000);
all_email_list varchar2(4000);

begin

select listagg(EMAIL,',') into first_email_list from UM_USER a left join UM_USERROLLE b on (a.mynetuser=b.NT_NAME) left join UM_RULES c on (c.id=b.RULEID) where RULEID = 902;
select listagg(EMAIL,',') into second_email_list from table2 where CFT_ID =:P25_CFT_TEAM;
select EMAIL into third_email_list from table3 WHERE :P25_ID = ID;


all_email_list:= first_email_list || ',' || second_email_list || ',' || third_email_list;

dbms_output.put_line(all_email_list);
end;

有什么方法可以简单地解决这个问题吗?可能是正则表达式。

最佳答案

解决方案说明。使用 CTE 首先将电子邮件列表拆分为每行 1 个电子邮件地址的行 (testd_rows)。然后从 testd_rows 中选择不同的行(testd_rows_unique),最后用 listagg 将它们重新组合在一起。从 19c 开始,您可以将 LISTAGGDISTINCT 关键字一起使用。

set serveroutput on size 999999
clear screen
declare

all_email_list varchar2(4000);
l_unique_email_list varchar2(4000);


begin
all_email_list := 'test@asd.com, test2@asd.com,test@asd.com,test3@asd.com, test4@asd.com,test2@asd.com';

WITH testd_rows(email) AS
(
select regexp_substr (all_email_list, '[^, ]+', 1, rownum) split
from dual
connect by level <= length (regexp_replace (all_email_list, '[^, ]+')) + 1
), testd_rows_unique(email) AS
(
SELECT distinct email FROM testd_rows
)
SELECT listagg(email, ',') WITHIN GROUP (ORDER BY email)
INTO l_unique_email_list
FROM testd_rows_unique;

dbms_output.put_line(l_unique_email_list);
end;
/

test2@asd.com,test3@asd.com,test4@asd.com,test@asd.com

但是...为什么要将行转换为逗号分隔的字符串,然后对其进行重复数据删除?使用 UNION 在单个 SELECT 语句中取出重复值,并对这些值执行 LISTAGG。那时不需要正则表达式。 UNION 将跳过重复项,而 UNION ALL 则返回所有行。

DECLARE
all_email_list varchar2(4000);
BEGIN
WITH all_email (email) AS
(
select email from UM_USER a left join UM_USERROLLE b on (a.mynetuser=b.NT_NAME) left join UM_RULES c on (c.id=b.RULEID) where RULEID = 902
UNION
select email from table2 where CFT_ID =:P25_CFT_TEAM
UNION
select email from table3 WHERE :P25_ID = ID
)
SELECT listagg(email, ',') WITHIN GROUP (ORDER BY email)
INTO all_email_list
FROM all_email;

dbms_output.put_line(all_email_list);
END;
/

关于sql - 从 Oracle 中的逗号分隔变量中删除重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69163651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com