我在列中有以下值,用逗号分隔。
BHOP23,BHOP23,BHOP24
我想知道值是否在列中重复。
我该怎么做?
答案 0 :(得分:3)
Oracle安装程序:
CREATE TABLE your_table ( your_list_column ) AS
SELECT 'a,a,b,c,d' FROM DUAL UNION ALL -- duplicates both at head
SELECT 'a,b,a,c,d' FROM DUAL UNION ALL -- duplicates at head and middle
SELECT 'a,b,c,d,a' FROM DUAL UNION ALL -- duplicates at head and tail
SELECT 'a,b,b,c,d' FROM DUAL UNION ALL -- duplicates at middle and next item
SELECT 'a,b,c,b,d' FROM DUAL UNION ALL -- duplicates at middle and middle
SELECT 'a,b,c,d,b' FROM DUAL UNION ALL -- duplicates at middle and tail
SELECT 'a,b,c,d,d' FROM DUAL UNION ALL -- duplicates both at tail
SELECT 'a,b,a,c,b' FROM DUAL UNION ALL -- two pairs of duplicates
SELECT 'a,b,c,d,e' FROM DUAL; -- no duplicates
要获取具有重复值的列表,可以在正则表达式中使用反向引用:
SELECT *
FROM your_table
WHERE REGEXP_LIKE( ',' || your_list_column || ',', ',([^,]+),(.+,)?\1,' )
<强>输出强>:
YOUR_LIST_COLUMN
----------------
a,a,b,c,d
a,b,a,c,d
a,b,c,d,a
a,b,b,c,d
a,b,c,b,d
a,b,c,d,b
a,b,c,d,d
a,b,a,c,b
要获得第一个重复值,您可以提取上述正则表达式的第一个子组:
SELECT your_list_column,
REGEXP_SUBSTR( ',' || your_list_column || ',', ',([^,]+),(.+,)?\1,', 1, 1, NULL, 1 )
AS duplicate_value
FROM your_table
WHERE REGEXP_LIKE( ',' || your_list_column || ',', ',([^,]+),(.+,)?\1,' )
<强>输出强>:
YOUR_LIST_COLUMN DUPLICATE VALUE
---------------- ---------------
a,a,b,c,d a
a,b,a,c,d a
a,b,c,d,a a
a,b,b,c,d b
a,b,c,b,d b
a,b,c,d,b b
a,b,c,d,d d
a,b,a,c,b a
要获取唯一值,请使用split_string()
function as defined here(但使用用户定义的类型而不是预定义的VARRAY
):
CREATE OR REPLACE TYPE stringlist IS TABLE OF VARCHAR2(4000);
/
CREATE OR REPLACE FUNCTION split_String(
i_str IN VARCHAR2,
i_delim IN VARCHAR2 DEFAULT ','
) RETURN stringlist DETERMINISTIC
AS
p_result stringlist := stringlist();
p_start NUMBER(5) := 1;
p_end NUMBER(5);
c_len CONSTANT NUMBER(5) := LENGTH( i_str );
c_ld CONSTANT NUMBER(5) := LENGTH( i_delim );
BEGIN
IF c_len > 0 THEN
p_end := INSTR( i_str, i_delim, p_start );
WHILE p_end > 0 LOOP
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, p_end - p_start );
p_start := p_end + c_ld;
p_end := INSTR( i_str, i_delim, p_start );
END LOOP;
IF p_start <= c_len + 1 THEN
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, c_len - p_start + 1 );
END IF;
END IF;
RETURN p_result;
END;
/
然后您可以将它与SET()
集合函数结合使用:
SELECT t.*,
(
SELECT LISTAGG( COLUMN_VALUE, ',' ) WITHIN GROUP ( ORDER BY ROWNUM )
FROM TABLE( SET( split_string( t.your_list_column ) ) )
) AS unique_list
FROM your_table t
<强>输出强>:
YOUR_LIST_COLUMN UNIQUE_LIST
---------------- ---------------
a,a,b,c,d a,b,c,d
a,b,a,c,d a,b,c,d
a,b,c,d,a a,b,c,d
a,b,b,c,d a,b,c,d
a,b,c,b,d a,b,c,d
a,b,c,d,b a,b,c,d
a,b,c,d,d a,b,c,d
a,b,a,c,b a,b,c
a,b,c,d,e a,b,c,d,e
答案 1 :(得分:1)
PL / SQL函数,它只依赖于简单的字符串函数(SUBSTR
,INSTR
和LENGTH
)来删除字符串列表中的重复项:
Oracle 11g R2架构设置:
CREATE TABLE your_table ( your_list_column ) AS
SELECT 'a,a,b,c,d' FROM DUAL UNION ALL -- duplicates both at head
SELECT 'a,b,a,c,d' FROM DUAL UNION ALL -- duplicates at head and middle
SELECT 'a,b,c,d,a' FROM DUAL UNION ALL -- duplicates at head and tail
SELECT 'a,b,b,c,d' FROM DUAL UNION ALL -- duplicates at middle and next item
SELECT 'a,b,c,b,d' FROM DUAL UNION ALL -- duplicates at middle and middle
SELECT 'a,b,c,d,b' FROM DUAL UNION ALL -- duplicates at middle and tail
SELECT 'a,b,c,d,d' FROM DUAL UNION ALL -- duplicates both at tail
SELECT 'a,b,a,c,b' FROM DUAL UNION ALL -- two pairs of duplicates
SELECT 'a,b,c,d,e' FROM DUAL -- no duplicates
/
CREATE OR REPLACE FUNCTION remove_Duplicates_From_List(
i_str IN VARCHAR2,
i_delim IN VARCHAR2 DEFAULT ','
) RETURN VARCHAR2 DETERMINISTIC
AS
p_result VARCHAR2(4000) := i_delim;
p_temp VARCHAR2(4000);
p_start NUMBER(5) := 1;
p_end NUMBER(5);
c_len CONSTANT NUMBER(5) := LENGTH( i_str );
c_ld CONSTANT NUMBER(5) := LENGTH( i_delim );
BEGIN
IF c_len > 0 THEN
p_end := INSTR( i_str, i_delim, p_start );
WHILE p_end > 0 LOOP
p_temp := SUBSTR( i_str, p_start, p_end + c_ld - p_start );
IF INSTR( p_result, i_delim || p_temp ) = 0 THEN
p_result := p_result || p_temp;
END IF;
p_start := p_end + c_ld;
p_end := INSTR( i_str, i_delim, p_start );
END LOOP;
IF p_start <= c_len + 1 THEN
p_temp := SUBSTR( i_str, p_start, c_len - p_start + 1 ) || i_delim;
IF INSTR( p_result, i_delim || p_temp ) = 0 THEN
p_result := p_result || p_temp;
END IF;
END IF;
END IF;
RETURN SUBSTR( p_result, c_ld + 1, LENGTH( p_result ) - 2 * c_ld );
END;
/
查询1 :
SELECT your_list_column,
remove_Duplicates_From_List( your_list_column ) AS uniq
FROM your_table
<强> Results 强>:
| YOUR_LIST_COLUMN | UNIQ |
|------------------|-----------|
| a,a,b,c,d | a,b,c,d |
| a,b,a,c,d | a,b,c,d |
| a,b,c,d,a | a,b,c,d |
| a,b,b,c,d | a,b,c,d |
| a,b,c,b,d | a,b,c,d |
| a,b,c,d,b | a,b,c,d |
| a,b,c,d,d | a,b,c,d |
| a,b,a,c,b | a,b,c |
| a,b,c,d,e | a,b,c,d,e |