我有一个字符串'1,1,2,3,4,4,5,6,6,7'存储在一列中。 我需要使用sql查询将不同的逗号分隔值作为输出。
e.g。对于给定的字符串输出应为'1,2,3,4,5,6,7'。输出中没有重复项。
答案 0 :(得分:3)
没有正则表达式:
WITH t AS
( SELECT '1,2,3,3,3,4,5,6,7,7,7,7' AS num FROM dual
)
SELECT DISTINCT
SUBSTR (
num
, instr(num, ',', 1, level) + 1
, instr(num, ',', 1, level + 1) - instr(num, ',', 1, level) - 1)
AS numbers
FROM (select ','||num||',' num from t)
CONNECT BY level <= length(num) - length(replace(num,',')) -1
使用正则表达式:
SELECT DISTINCT REGEXP_SUBSTR( '1,1,2,3,4,4,5,6,6,7' , '[^,]+', 1, lvl)
FROM DUAL,
(SELECT LEVEL lvl
FROM DUAL
CONNECT BY LEVEL <= LENGTH( '1,1,2,3,4,4,5,6,6,7' ) - LENGTH(REPLACE( '1,1,2,3,4,4,5,6,6,7' , ','))+1)
WHERE lvl <= LENGTH( '1,1,2,3,4,4,5,6,6,7' ) - LENGTH(REPLACE( '1,1,2,3,4,4,5,6,6,7' , ',')) + 1
答案 1 :(得分:0)
我们可以使用regex_substr和connect by来做到这一点。请试试这个。
select distinct num from
(SELECT REGEXP_SUBSTR('1,1,2,3,4,4,5,6,6,7','[^,]+',1,level) as num
FROM DUAL
CONNECT BY LEVEL<= LENGTH(REGEXP_REPLACE('1,1,2,3,4,4,5,6,6,7','[^,]','')));
没有正则表达式: 经过一些问题的澄清
with t as (SELECT distinct substr(replace('1,1,2,3,4,4,5,6,6,7',','),level,1)||',' as num
FROM DUAL
CONNECT BY LEVEL<= LENGTH( '1,1,2,3,4,4,5,6,6,7' ) - LENGTH(REPLACE( '1,1,2,3,4,4,5,6,6,7' , ','))+1)
select listagg(num) within group (order by num) from t;
答案 2 :(得分:0)
尝试
select
regexp_replace('1,1,2,3,4,4,5,6,6,7', '([^,]+),\1', '\1')
from
dual;
但是,如果您的输入字符串包含的图形超过两次,则此方法不起作用。如果这困扰你,你可能想尝试
select
regexp_replace('1,1,2,3,4,4,4,5,6,6,6,6,6,6,7', '([^,]+)(,\1)+', '\1')
from dual;
答案 3 :(得分:0)
根据我的观点:
select wm_concat(distinct substr(replace('1,1,2,3,4,4,5,6,6,7',',',''),level,1)) as out
from dual connect by level <= length('1,1,2,3,4,4,5,6,6,7');
答案 4 :(得分:0)
SELECT
listagg(ra,',') WITHIN GROUP (ORDER BY ra)
FROM
(
SELECT
DISTINCT (REGEXP_SUBSTR('02,02,02,02,02,03,04,03', '[^,]+', 1, LEVEL) )ra
FROM DUAL
CONNECT BY REGEXP_SUBSTR('02,02,02,02,02,03,04,03', '[^,]+', 1, LEVEL) IS
NOT NULL
);