將多個逗號分隔值拆分為多個Oracle 表行且不重複
使用Oracle CONNECT BY 將逗號分隔資料拆分為多行時和正規表示式,可能會遇到重複的行。以下是產生重複結果的原始查詢:
WITH CTE AS ( SELECT 'a,b,c,d,e' temp, 1 slno FROM DUAL UNION SELECT 'f,g', 2 from dual UNION SELECT 'h', 3 FROM DUAL ) SELECT TRIM(REGEXP_SUBSTR(TEMP, '[^,]+', 1, LEVEL)), SLNO FROM CTE CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(temp, '[^,]+')) + 1
為了解決重複行的問題,對查詢進行了以下修改:
WITH CTE AS ( SELECT 'a,b,c,d,e' temp, 1 slno FROM DUAL UNION SELECT 'f,g' temp, 2 slno FROM DUAL UNION SELECT 'h' temp, 3 slno FROM DUAL ) SELECT TRIM(REGEXP_SUBSTR(temp, '[^,]+', 1, level)), slno FROM CTE CONNECT BY level <= REGEXP_COUNT(temp, '[^,]+') AND PRIOR slno = slno AND PRIOR DBMS_RANDOM.VALUE IS NOT NULL
說明:
以上是在 Oracle 中拆分逗號分隔值時如何避免重複行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!