SQL - 两个列表之间的点差异

时间:2017-03-15 10:17:23

标签: sql regex vertica

给定两个逗号分隔(无序)的数字列表,我想只提取它们之间的差异(可能使用regexp)。 e.g:

select '1010484,1025781,1051394,1069679' as list_1, '1005923,1010484,1025781,1034010,1044261,1048311,1051394' as list_2

我希望得到的结果如下:

l1_additional_data:1069679

l2_additional_data:1005923,1034010,1044261,1048311

如何做到这一点?

我使用的是Vertica,BTW - 这意味着可以在这里使用没有层次结构("通过"连接)查询。

提前致谢!

2 个答案:

答案 0 :(得分:0)

有一篇相关的帖子很有帮助 - Splitting string into multiple rows in Oracle

我不知道vertica但是基于oracle你可以选择:

with list1 as
(
select 
regexp_substr(list_1 ,'[^,]+', 1, level) as list_1_rows
from  (
select
'1010484,1025781,1051394,1069679' as list_1
from dual)
connect by 
  regexp_substr(list_1 ,'[^,]+', 1, level) is not null),

list2 as (select 
regexp_substr(list_2 ,'[^,]+', 1, level) as list_2_rows
from  (
select
'1005923,1010484,1025781,1034010,1044261,1048311,1051394' as list_2
from dual)
connect by regexp_substr(list_2 ,'[^,]+', 1, level) is not null) 

select * from list1
full outer join list2 
on list1.list_1_rows = list2.list_2_rows
where list_1_rows is null or list_2_rows is null

答案 1 :(得分:0)

好的,这是我的解决方案 - 但它效率不高,而且可能无法扩展(在性能方面):

WITH lists AS (SELECT'1010484,1025781,1051394,1069679' AS list_1, '1005923,1010484,1025781,1034010,1044261,1048311,1051394' AS list_2 ) , numbers AS (SELECT row_number() over() i FROM system_columns limit 100) SELECT group_concat(parsed_code_1) list_1_additions, group_concat(parsed_code_2) list_2_additions FROM(SELECT parsed_code_1 FROM(SELECT split_part(list_1, ',', i) parsed_code_1 FROM lists CROSS JOIN numbers WHERE i <= regexp_count(list_1, ',')+1) l WHERE parsed_code_1 IS NOT NULL) a FULL OUTER JOIN (SELECT parsed_code_2 FROM(SELECT split_part(list_2, ',', i) parsed_code_2 FROM lists CROSS JOIN numbers WHERE i <= regexp_count(list_2, ',')+1) l WHERE parsed_code_2 IS NOT NULL) b ON(parsed_code_1 = parsed_code_2) WHERE parsed_code_1 IS NULL OR parsed_code_2 IS NULL