How to remove duplicates from comma separated string


Hi all

I have an obligation to remove duplicates of string separated by commas:

Some examples of code (real data can be in millions)

{code}

create table test_dup_del (num number (10), dup_val varchar2 (100));

insert into test_dup_del values (1234, '1, 2, 3, 4, 3, 1');
insert into test_dup_del values (4567,'1, 2, 2, 4, 4, 1');
insert into test_dup_del values (7894,'1, 2, 1, 3, 3, 1');
insert into test_dup_del values (1478, '1, 2, 4, 4, 3, 2');
insert into test_dup_del values (2589, '1, 2, 1, 2, 1, 2');
commit;

(code}

my output would be:

1234, 1, 2, 3, 4'

4567, '1, 2, 4'

7894, '1, 2, 3'

1478, 1, 2, 3, 4'

2589, 1, 2'

Thank you

Rachid

If there is a real need to split the data so that it could be corrected (before split to store correctly... Tip tip!) ...

SQL > ed
A written file afiedt.buf

1 with test_dup_del (num, dup_val) as)
2 Select 1234, 1, 2, 3, 4, 3, 1' Union double all the
3 select 4567, 1, 2, 2, 4, 4, 1' Union double all the
4 Select 7894, 1, 2, 1, 3, 3, 1' Union double all the
5 Select 1478, 1, 2, 4, 4, 3, 2' of all the double union
6 select 2589, 1, 2, 1, 2, 1, 2' of the double
7      )
8  --
9. end of test data
10-
11, split as)
12. Select num
13, trim (regexp_substr(dup_val,'[^,]+',1,level)) as val
14 from test_dup_del
15 connect by num = num prior
16 and level<=>
17 and prior sys_guid() is not null
18                )
19, dist in (select distinct)
20                      num
21                      ,val
22 split
23              )
24 select num
25, listagg (val, ',') within the Group (order by val) as vals
dist 26
27 * group by num
SQL > /.
NUM OF VALS
---------- ------------------------------
1234 1,2,3,4
1478 1,2,3,4
2589 1.2
4567 1,2,4
7894 1,2,3

5 selected lines.

Tags: Database

Similar Questions

  • Remove duplicates of comma separated String

    Hello
    I have a column that contains the values of duplicates with separated by commas.

    Customer ID
    -----------------------------
    5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2

    I wrote this:

    Select REGEXP_REPLACE ('5, 5, 5, 5, 6, 6, 5, 5, 5, 6, 7, 4, 1, 2, 1, 4, 7, 2', ' (^ |,)([^,]*) (, \2)+','\1\2') of double;)
    ---------------------------------
    5,6,5,6,7,4,1,2,1,4,7,2

    But it eliminates only the continuous elements.
    I want to put it as:
    ---------------------
    5,6,7,4,1,2

    Help, please.
    Thank you.
    Amit

    Published by: 987565 on February 12, 2013 04:02

    Since you don't have 11 GR 2, making it a lot easier, I would use INSTR/SUBSTR to break the values, and then use START WITH / CONNECT BY to group the separate values of back together. I avoid the REGEXP stuff when I can because he uses a lot of CPU.

    WITH INDATA AS (
      select '9,9,8,8,7,7,6,6,5,5,4,4,3,3,2,2,1,1' x from dual
      union all
      select '9,9,8,8,7,7,4,4,3,3,2,2,1,1' x from dual
    ), TOKENIZED AS (
      SELECT DISTINCT A.INNUM,
      dense_rank() over(partition by innum order by b.column_value) x_rank,
      B.COLUMN_VALUE x
      FROM (select rownum innum, x from indata) a,
      table(cast(multiset(
        SELECT trim(SUBSTR(
          x,
          (case level
            when 1 then 1
            else instr(x, ',', 1, level-1) + 1
          end),
          (case instr(x, ',', 1, level)
            when 0 then 4000
            else instr(x, ',', 1, level) - 1 -
              (case level
                when 1 then 0
                else instr(x, ',', 1, level-1)
              end)
          end)
        ))
        FROM dual
        CONNECT BY level <= LENGTH(x) - LENGTH(REPLACE(x, ',', '')) + 1
      ) AS SYS.ODCIVARCHAR2LIST)) B
    )
    SELECT ltrim(sys_connect_by_path(x, ','),',') x
    FROM TOKENIZED A
    where connect_by_isleaf = 1
    START WITH X_RANK = 1
    connect by (innum, x_rank) = ((prior innum, prior x_rank + 1));
    
    X
    --------------------
    1,2,3,4,5,6,7,8,9
    1,2,3,4,7,8,9
    

    Published by: stew Ashton on February 13, 2013 15:47

  • How to remove duplicates from a Table using PL/SQL

    I created the following tables with data:

    * Table 1: *.



    Department of CREATE TABLE

    (DEPARTMENT_ID 4 NUMBER PRIMARY KEY,

    DEPARTMENT_NAME VARCHAR2 (20).

    ADDRESS VARCHAR2 (20));







    INSERT INTO Department VALUES (10, 'ACCOUNTING', 'NEW YORK');

    INSERT INTO Department VALUES (20, "SEARCH", "DALLAS");

    INSERT INTO Department VALUES (30, 'SALES', 'CHICAGO');

    INSERT INTO Department VALUES (40, 'OPERATIONS', 'BOSTON');

    COMMIT;



    * 2nd table: *.

    CREATE TABLE employee

    (EMPLOYEE_ID NUMBER 4 PRIMARY KEY,

    EMPLOYEE_NAME VARCHAR2 (20).

    USE VARCHAR2 (20).

    MANAGER_ID NUMBER 4,

    HIRE_DATE DATE,

    NUMBER OF SALARY (9, 2)

    COMMISSION (9, 2) NUMBER,

    Department_id number 4 REFERENCES department (DEPARTMENT_ID));






    INSERT INTO employee

    VALUES (7839, 'KING', 'PRESIDENT', NULL, NOVEMBER 17, 81 ', 5000, NULL, 10);

    INSERT INTO employee

    VALUES (7698, "BLAKE", "MANAGER", 7839, MAY 1, 81 ', 2850, NULL, 30);

    INSERT INTO employee

    VALUES (7782, "CLARK", "MANAGER", 7839, JUNE 9, 81 ', 2450, NULL, 10);

    INSERT INTO employee

    VALUES (7566, "JONES", "MANAGER", 7839, APRIL 2, 81 ', 2975, NULL, 20);

    INSERT INTO employee

    VALUES (7654, 'MARTIN', 'SELLER', 7698, 28-SEP-81', 1250, 1400, 30);

    INSERT INTO employee

    VALUES (7499, 'ALLEN', 'SELLER', 7698, FEBRUARY 20, 81 ', 1600, 300, 30);

    INSERT INTO employee

    VALUES (7844, 'TURNER', 'SELLER', 7698, ' 08-SEP-81', 1500, NULL, 30);

    INSERT INTO employee

    VALUES (7900, 'JAMES', 'CLERKS', 7698, 3 DECEMBER 81', 950, NULL, 30);

    INSERT INTO employee

    VALUES (7521, 'WARD', 'SELLER', 7698, FEBRUARY 22, 81 ', 1 250, 500, 30);

    INSERT INTO employee

    VALUES (7902, "FORD", "ANALYST", 7566, DECEMBER 3, 81 ', 3000, NULL, 20);

    INSERT INTO employee

    VALUES (7369, 'SMITH', 'CLERKS', 7902, DECEMBER 17, 81 ', 800, NULL, 20);

    INSERT INTO employee

    VALUES (7788, 'SCOTT', 'ANALYST', 7566, DECEMBER 9, 82 ', 3000, NULL, 20);

    INSERT INTO employee

    VALUES (7876, ' ADAMS, "CLERK", 7788, 12 JANUARY, 83', 1100, NULL, 20);

    INSERT INTO employee

    VALUES (7934, 'MILLER', 'CLERKS', 7782, JANUARY 22, 82 ', 1300, NULL, 10);

    COMMIT;



    I need to print all Department names, employee and salary. I did this, but how can display the name of each Department only once?





    Here is my code:



    DECLARE

    v_department_name department.department_name%TYPE;

    v_employee_name employee.employee_name%TYPE;

    v_salary employee.salary%TYPE;

    CURSOR dept_cur

    IS

    SELECT employee_name, department_name, Department OF employee salary

    WHERE department.department_id = employee.department_id (+)

    ORDER BY department_name, employee_name;

    BEGIN

    OPEN dept_cur.

    EXTRACT the dept_cur IN v_department_name, v_employee_name, v_salary;

    DBMS_OUTPUT. PUT_LINE ('DEPARTMENT_NAME EMPLOYEE_NAME WAGES');

    DBMS_OUTPUT. PUT_LINE('---------------------------------------------------');

    While dept_cur % FOUND

    LOOP

    DBMS_OUTPUT. Put_line (RPAD (v_department_name, 10) |) ' ' || RPAD (v_employee_name, 10). ' ' || v_salary);

    EXTRACT the dept_cur IN v_department_name, v_employee_name, v_salary;

    END LOOP;

    CLOSE Dept_cur;

    END;

    I would appreciate if you could help me with this. I'm sure it's a quick and easy answer, but I can't do it. I spent so much time already.

    Published by: 831522 on January 26, 2011 11:44

    It is a problem of display/reports rather than a problem of duplication of query data. The answer depends on what tools you use to query and display the data. You can use the clause "break it" I suggested earlier if you use SQL Plus.

    Otherwise, you can try this based on the version of Oracle you are using. Someone else may be able to get the data in a single pass.

    SELECT decode(emp_id, 1, department_name) department_name, employee_name, salary
    FROM(SELECT department_name, employee_name, salary,
           ROW_NUMBER() OVER (PARTITION BY department_name order by department_name, employee_name) AS emp_id
         FROM department
         LEFT OUTER JOIN employee ON (department.department_id = employee.department_id)
    ORDER BY department_name, employee_name, emp_id);
    
  • How to remove duplicates from the PL - SQL table?

    Hi gurus,

    I have a PL - SQL table with the following structure
    Authors (SR_NO, Auth_Code, Change_Date, cost)

    This table is filled using a slider. However, this table can have a few lines in double (for column (Auth_Code)
    for example
    SR_NO      Auth_Code       Change_Date                       Cost
    1               A1             14-FEB-09 08.18.47 AM          11.00
    2               A2             14-FEB-09 08.18.56 AM       2839.00
    3               A1             15-FEB-09 08.00.02 AM      1299.00
    4               A1             15-FEB-09 07.00.00 AM        789.00
    5               A3             14-FEB-09 08.18.56 AM        312.00
    6               A4             14-FEB-09 08.19.02 AM        233.00
    I need to get the above result set select the separate lines of Auth_Code including the Change_Date is maximum (and store in another PL - SQL table for treatment later or even the removal of this table will be also!)

    of the data A1 is duplicated and a maximum Change_Date above = 15 February 09 08.00.02 AM.
    Where my PL - SQL Table that results must have given below
    SR_NO      Auth_Code       Change_Date                       Cost
    2               A2             14-FEB-09 08.18.56 AM       2839.00
    3               A1             15-FEB-09 08.00.02 AM      1299.00
    5               A3             14-FEB-09 08.18.56 AM        312.00
    6               A4             14-FEB-09 08.19.02 AM        233.00
    I'm not very aware of the PL - SQL tables and there is no chance to change the existing cursor that fills the data in this table PL - SQL.
    I guess that I need to compare each record of PL - SQL table with others, but do not know how to do this.

    Could you please help?

    Hello

    Like this?:

    Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
    Connected as hr
    
    SQL>
    SQL> with data as(
      2  select 1 as SR_NO, 'A1' as Auth_Code, to_date('14-FEB-09 08.18.47', 'dd-mon-yy hh24:mi:ss') as change_date,    11.00 as cost from dual union all
      3  select 2 as SR_NO, 'A2' as Auth_Code, to_date('14-FEB-09 08.18.56', 'dd-mon-yy hh24:mi:ss') as change_date,  2839.00 as cost from dual union all
      4  select 3 as SR_NO, 'A1' as Auth_Code, to_date('15-FEB-09 08.00.02', 'dd-mon-yy hh24:mi:ss') as change_date,  1299.00 as cost from dual union all
      5  select 4 as SR_NO, 'A1' as Auth_Code, to_date('15-FEB-09 07.00.00', 'dd-mon-yy hh24:mi:ss') as change_date,   789.00 as cost from dual union all
      6  select 5 as SR_NO, 'A3' as Auth_Code, to_date('14-FEB-09 08.18.56', 'dd-mon-yy hh24:mi:ss') as change_date,   312.00 as cost from dual union all
      7  select 6 as SR_NO, 'A4' as Auth_Code, to_date('14-FEB-09 08.19.02', 'dd-mon-yy hh24:mi:ss') as change_date,   233.00 as cost from dual)
      8  select * from data d where change_date = (select max(change_date) from data d2 where d.auth_code = d2.auth_code);
    
         SR_NO AUTH_CODE CHANGE_DATE       COST
    ---------- --------- ----------- ----------
             2 A2        14/02/2009        2839
             3 A1        15/02/2009        1299
             5 A3        14/02/2009         312
             6 A4        14/02/2009         233
    
    SQL>
    

    Kind regards

  • comma-separated string

    I have a string that comes an external instrument. When it is read, he appears in a format like this:

    "10,55.

    11.25

    13.43

    15.68 "

    The number of entries varies. I need to get this string in a comma-separated string that is later concatenated with another string and written to a csv file.

    So how do the entry above into this: ' 10,55, 11.25, 13.43 15.68?

    Thank you

    There are a lot of ways. I use find and replace. See attachemt for an example.

  • Windows Live Movie Maker 2011 - how to remove items from the recent projects list

    Windows Live Movie Maker 2011

    I have duplicates on my list of recent projects.

    How to remove items from the numbered list of recent projects?  What are the (thumb)

    bugs used for?  Thank you.

    Hello

    The question you have posted is related to Windows Live Movie Maker and would be better suited in the Windows Live Solution Center. Please visit the link below to find a community that will provide the best support.

    http://www.windowslivehelp.com/product.aspx?ProductID=5

    Amrita M

    Microsoft Answers Support Engineer

  • How to remove duplicate files/songs in my music library without having to click on each of them?

    original title: removal of duplicates

    How to remove duplicate files/songs in my music library without having to click on each of them?

    Hi MonicaBlanco,

    1 are. what music library you referring?

    2. did you of recent changes on the computer?

    If you are referring to the Windows media player library then the only option to remove duplicates of files is to click with the right button on the file duplicate.

    Remove items from the Windows Media Player library

    http://Windows.Microsoft.com/en-us/Windows7/remove-items-from-the-Windows-Media-Player-library

  • Adding zero in comma separated string

    Hi all

    Currently I am working on Oracle 11 g

    I have the string as 1,12,123,1234 and my requirement is to fill the zero on every comma separated string

    My output result should be like this 0001,0012,0123,1234, what exactly does lpad('12',4,'0').

    Kindly help a query above.

    A simple trick I learned for a long time back here

    SQL> with t
      2  as
      3  (
      4  select '1,12,123,1234' str from dual
      5  )
      6  select regexp_replace(regexp_replace(str, '([[:digit:]]+)', '0000\1'), '0+([[:digit:]]{4})', '\1') str
      7    from t;
    
    STR
    -------------------
    0001,0012,0123,1234
    
    SQL>
    
  • How to remove photos from my iphone6s after their importation?

    How to remove photos from my iphone6s after their importation?

    Hello KConstant,

    I understand that you're looking to remove photos from your iPhone, after you import the to your Mac. It depends largely on how the images are imported into your Mac.

    If you use iCloud photo library, remove photos from your iPhone, after they have synched to the Photos on your Mac afterwards would remove these photos on your Mac as well, like iCloud photo library is designed to synchronize your iPhone with iCloud and Mac.

    However, if you are importing photos via the USB port of the Photos app, you should be prompted to delete pictures from your device once the import is complete:

    Once the import is complete, a message asks if you want to delete or keep the photos on your device. Click on remove items to remove photos or click on keep items to keep them on the device.

    Import photos and videos from your iPhone, iPad or iPod touch
    https://support.Apple.com/en-us/HT201302

    You can also (it not allowed you iCloud photo library on your iPhone), you can use the image capture on your Mac app to delete images from your iPhone. This can also be useful to remove the last images that have already been imported into the Photos on your Mac. Once you have selected the images you want in the Image Capture, you can click on the prohibitive red symbol () to delete the selected items.

    Image capture: transfer images and other elements of your device
    https://support.Apple.com/kb/PH17894

    Concerning

  • How to remove ps4ux from my mac?

    How to remove ps4ux from my macbook pro?

    1. use Malwarebytes Anti-Malware for Mac to remove adware

    https://www.Malwarebytes.org/antimalware/Mac/

    Download, install, open and run by clicking on the "Scan for Adware" button to remove the adware.

    Once this is done, exit Malwarebytes Anti-Malware.

    2 Disable Extensions and test them.

    Safari > Preferences > Extensions

    Select Disable all extensions and test them.

    Enable the Extensions one by one and test.

    To uninstall any extension, select it and click the "Uninstall" button

    3 safari > Preferences > Search > search engine:

    Select your preferred search engine

    4 safari > Preferences > general > home page

    Visit the site of your choice that it be the homepage

    Set your home page.

  • How to remove photos from my iPhone without deleting icloud photo library?

    My iPhone is full and I wanted to remove some photo to release some spaceso I supported the up to iCloud and I started to remove photos. ***? They are endangered to iCloud! Now, they're just gone. It's crazy! Image capture you can import to everywhere, but the function "delete after importing" seems to have disappeared. I'm sure this question has been beat to death, but how you remove photos from their phone without removing them from their photo library iCloud?

    So frustrating.

    Does anyone know of a solution to this?

    iCloud is a synchronization system, not just external storage.  Once you turn on photo library all you do with a photo is on ALL of your devices (including remove them).  See the last section in this article on how to recover pictures that you inadvertently deleted:

    iCloud: use iCloud photo library on your iOS device

    It is also here:

    Get help with your photo library - Apple Support iCloud

    How to remove pictures and videos from a camera using iCloud photo library?

    If you want to delete the pictures and videos of all your iOS, Mac and iCloud.comdevices, you can delete them in the Photos app.

    When you delete a photo or video of your Photos app, also will not delete your photo library and iCloud iCloud storage. You can retrieve your photos and videos from the album recently deleted for 30 days.

  • I've upgraded to a newer version of iTunes.  When I opened the new iTunes, all of these songs randomly appeared in my iTunes playlist!  I have NO idea where they came from.  Why they are on my computer, and how to remove them from my iTunes?

    I've upgraded to a newer version of iTunes.  When I opened the new iTunes, all of these songs randomly appeared in my iTunes playlist!  I have NO idea where they came from.  Why they are on my computer, and how to remove them from my iTunes?

    I think iTunes might be showing your iCloud music purchases. Go to the menu bar, and unselect display-> show iCloud music purchases.

  • How to remove duplicate pictures

    I imported all my photos in the photo library. How to remove duplicates of Photos (version 1.3) on OS X El Capitan without 3rd party software?

    Photos don't have a tool to detect the duplicates library. It relies on detecting when you import photos.

    If you intentionally import duplicates, you can only search for them manually, for example by sorting the photos of the capture date, if duplicates will appear side-by-side in moments.

    To search for duplicates, you need third-party software. These three are safe to use with Photos:

  • How to remove applications from the startup folder

    How to remove applications from the startup folder

    Settings > users and groups > Login Items.

  • How to remove photos from a mac that I get rid of without losing these photos on devices that I keep

    How to remove photos from a mac that I get rid of without losing these photos on devices that I keep

    Do not use iCLoud photo library - when you make any changes on any device are made on all devices

    LN

Maybe you are looking for