


How to Avoid Duplicate Rows When Splitting Comma-Separated Values in Oracle?
Jan 01, 2025 am 05:30 AMSplitting Multiple Comma-Separated Values into Multiple Oracle Table Rows without Duplicates
When splitting comma-separated data into multiple rows using Oracle's CONNECT BY and regular expressions, it's possible to encounter duplicate rows. Below is the original query that was generating duplicate results:
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
To resolve the issue of duplicate rows, the following modifications were made to the query:
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
Explanation:
- The REGEXP_COUNT function is used to determine the number of comma-separated values in each string.
- The PRIOR operator ensures that the current row is connected to the previous row based on the slno column.
- The DBMS_RANDOM.VALUE function generates a random value and is used to introduce randomness in the partitioning process, which helps prevent duplicate rows from being generated.
The above is the detailed content of How to Avoid Duplicate Rows When Splitting Comma-Separated Values in Oracle?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library

Run MySQl in Linux (with/without podman container with phpmyadmin)

What is SQLite? Comprehensive overview

Running multiple MySQL versions on MacOS: A step-by-step guide

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

How do I configure SSL/TLS encryption for MySQL connections?
