Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to skip duplicates without sorting while append in an ITAB using new ABAP Code?

0 Likes
9,823

Hello Guys

I would like to append the entries from one table to an another table which has the same structure but append should skip the duplicate entries. How can I achieve it using new ABAP Coding?

Sample Code: Image1

Expected Result: Image 2

Regards,

Abhishek

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
7,488

That could be done quite simply with COLLECT, but as you want to do something with the "new" constructor expressions...

You need to mix a non-sorted list and a sorted list, so let's go for a table with a standard primary key and a unique (hashed here) secondary key, and a hack using CORRESPONDING DISCARDING DUPLICATES:

TYPES: BEGIN OF ty_header,
         vbeln TYPE vbeln_va,
       END OF ty_header.

TYPES gtt_header TYPE STANDARD TABLE OF ty_header WITH DEFAULT KEY
        WITH UNIQUE HASHED KEY by_dummy COMPONENTS vbeln.

DATA(gt_vbeln) = VALUE gtt_header(
                    ( vbeln = '4500000029' )
                    ( vbeln = '4500000028' )
                    ( vbeln = '4500000027' ) ).

DATA(gt_vbeln1) = VALUE gtt_header(
                    ( vbeln = '4500000026' )
                    ( vbeln = '4500000030' )
                    ( vbeln = '4500000029' ) ).

gt_vbeln = CORRESPONDING #( BASE ( gt_vbeln ) gt_vbeln1 DISCARDING DUPLICATES ).

ASSERT gt_vbeln = VALUE gtt_header(
                    ( vbeln = '4500000029' )
                    ( vbeln = '4500000028' )
                    ( vbeln = '4500000027' )
                    ( vbeln = '4500000026' )
                    ( vbeln = '4500000030' ) ).

Hello Guys

I would like to append the entries from one table to an another table which has the same structure but append should skip the duplicate entries. How can I achieve it using new ABAP Coding?

Sample Code: Image1

Expected Result: Image 2

Regards,

Abhishek

8 REPLIES 8
Read only

Peranandam
Contributor
7,488

Hello Here you go

 TYPES: BEGIN OF ty_vbeln,
          vbeln TYPE vbeln,
        END OF ty_vbeln,
       ty_t_vbeln TYPE SORTED TABLE OF ty_vbeln WITH UNIQUE KEY vbeln.


 DATA(gt_vbeln) = VALUE ty_t_vbeln( ( vbeln = '10' ) ( vbeln = '11' ) ( vbeln = '12' ) ) .
 DATA(gt_vbeln1) = VALUE ty_t_vbeln( ( vbeln = '14' ) ( vbeln = '11' ) ( vbeln = '12' ) ) .
 INSERT LINES OF FILTER #( gt_vbeln1 EXCEPT IN gt_vbeln WHERE vbeln = vbeln ) INTO TABLE gt_vbeln.
Read only

0 Likes
7,488

Hello peranandam

Thank you so much for your response, but I don't want to disturb the order of table lt_header. As you can see in Image2, I want the exact output. The unique entries from Lt_header1 should simply need to be appended in lt_header1 and lt_header cannot be sorted.

Regards

Abhishek

Read only

Sandra_Rossi
Active Contributor
7,488

Please post the code as text instead of image, so that one can easily answer by modifying portions of your code.

Read only

Sandra_Rossi
Active Contributor
7,488

Mentioning "without sorting" discards the interest of the question, how could the code find the duplicates efficiently without impairing the performance?

Read only

Sandra_Rossi
Active Contributor
7,488

To help you, to help others, thank you to peranandam:

TYPES: BEGIN OF ty_header,
         vbeln TYPE vbeln_va,
       END OF ty_header.

TYPES gtt_header TYPE STANDARD TABLE OF ty_header WITH DEFAULT KEY.

DATA(gt_vbeln) = VALUE gtt_header(
                    ( vbeln = '4500000029' )
                    ( vbeln = '4500000028' )
                    ( vbeln = '4500000027' ) ).

DATA(gt_vbeln1) = VALUE gtt_header(
                    ( vbeln = '4500000026' )
                    ( vbeln = '4500000030' )
                    ( vbeln = '4500000029' ) ).
Read only

FredericGirod
Active Contributor
0 Likes
7,488

you could copy the content of your internal table into another temporary internal table. Do a sort, check entry and add it to the final table.

Read only

Sandra_Rossi
Active Contributor
7,489

That could be done quite simply with COLLECT, but as you want to do something with the "new" constructor expressions...

You need to mix a non-sorted list and a sorted list, so let's go for a table with a standard primary key and a unique (hashed here) secondary key, and a hack using CORRESPONDING DISCARDING DUPLICATES:

TYPES: BEGIN OF ty_header,
         vbeln TYPE vbeln_va,
       END OF ty_header.

TYPES gtt_header TYPE STANDARD TABLE OF ty_header WITH DEFAULT KEY
        WITH UNIQUE HASHED KEY by_dummy COMPONENTS vbeln.

DATA(gt_vbeln) = VALUE gtt_header(
                    ( vbeln = '4500000029' )
                    ( vbeln = '4500000028' )
                    ( vbeln = '4500000027' ) ).

DATA(gt_vbeln1) = VALUE gtt_header(
                    ( vbeln = '4500000026' )
                    ( vbeln = '4500000030' )
                    ( vbeln = '4500000029' ) ).

gt_vbeln = CORRESPONDING #( BASE ( gt_vbeln ) gt_vbeln1 DISCARDING DUPLICATES ).

ASSERT gt_vbeln = VALUE gtt_header(
                    ( vbeln = '4500000029' )
                    ( vbeln = '4500000028' )
                    ( vbeln = '4500000027' )
                    ( vbeln = '4500000026' )
                    ( vbeln = '4500000030' ) ).
Read only

7,488

Hello Sandra

This is what I was expecting. Thankyou so much. It works like a charm in my main Logic.

Regards

Abhishek