ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Removing duplicate records

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Removing duplicate records

    I have a file, SFILE that has occasionally has fields SRORD# and SRLINE duplicated

    Click image for larger version

Name:	image.png
Views:	68
Size:	7.8 KB
ID:	159046

    I need to make a copy of this file with the records removed where fields SRORD# and SRLINE are duplicated. Can some suggest the SQL to do it.

    Thank you

    Attached Files

  • #2
    ... are the other column values identical in both records?
    If so, try:
    Code:
    CREATE TABLE YourSchema/YourFile
            as (Select Distinct Col1, Col2, .... ColN
                  from OldTable)
    with Data;

    Comment


    • #3
      If the other columns are not necessarily identical, try:
      Code:
      create table duplicatesTable as (
        select a.* from myTable a
          left exception join myTable b
          on (a.SRORD#, a.SRLINE) = (a.SRORD#, a.SRLINE)
           and rrn(a) < rrn(b)
      ) with data;
      If there are multiple rows with the same values for SRORD# and SRLINE, it will keep only the last one by relative record number. You could use some other criteria for determining which record to keep

      Comment


      • #4
        Sorry, there was a typo in my SQL ("... = (a.SRORD#, a.SRLINE)" should have been "... = (b.SRORD#, b.SRLINE)" ), and I cannot edit my replies. Corrected SQL below:

        Code:
        create table duplicatesTable as (
          select a.* from myTable a
            left exception join myTable b
            on (a.SRORD#, a.SRLINE) = (b.SRORD#, b.SRLINE)
             and rrn(a) < rrn(b)
        ) with data;

        Comment

        Working...
        X