将多个逗号分隔值拆分为多个 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中文网其他相关文章!