简体   繁体   中英

SELECT only Unique values from Multiple Columns in SQL

I have to concatenate around 35 Columns in a table into a single string. The data within a column can be repetitive with different case, as per the below.

COL_1
apple | ORANGE | APPLE | Orange 

COL_2
GRAPE | grape | Grape

The data in each column is pipe separated and I am trying to concatenate each column by separating with '|' . I expect the final output to be "apple | orange | grape" (All in lower case is fine)

But currently I am getting

apple | ORANGE | APPLE | Orange | GRAPE | grape | Grape

My current SQL is

SELECT COL_1 || '|' || COL_2 from TABLE_X;

Can some one explain me how to extract unique value from each column? This will reduce my string length drastically. My current SQL is exceeding Oracle's 4000 character limit.

I tried doing this

WITH test AS ( SELECT 'Test | test | Test' str FROM dual ) SELECT * FROM (SELECT DISTINCT(LOWER(regexp_substr (str, '[^ | ]+', 1, rownum))) split FROM test CONNECT BY level <= LENGTH (regexp_replace (str, '[^ | ]+')) + 1 ) WHERE SPLIT IS NOT NULL;

This query produces only 'test'

Some how its producing unique values after splitting the string separated by ' | ' in a column. But doing this for 35+ columns in a single SQL query would be cumbersome. Could someone suggest a better approach?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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