6 Columns with Active Background
6 Columns with Active Background
Active List Item Background

24DDIC2707 – Efficient Data Management Sorting and Removing Adjacent Duplicates

In the world of ABAP programming, effectively managing and processing data is key to building robust applications. One common requirement is to sort data and remove adjacent duplicates from internal tables. This post will guide you through the process of sorting data and eliminating adjacent duplicate entries using ABAP’s powerful internal table operations.

Sorting Data

Sorting internal tables in ABAP is straightforward and efficient using the ‘SORT’ statement. This statement arranges the entries of an internal table based on the fields you specify. Sorting is crucial for organizing data and is often a precursor to other operations such as filtering or removing duplicates.

Here’s an example of how to sort an internal table:

DATA: lt_my_table TYPE TABLE OF my_structure.

” Populate lt_my_table with data

SORT lt_my_table BY field1 field2.

In this example, ‘lt_my_table’ is sorted first by ‘field1’ and then by ‘field2’. The ‘SORT’ statement ensures that the data in ‘lt_my_table’ is arranged in ascending order according to these fields. If you need descending order sorting you can use the ‘DESCENDING’ keyword:

SORT lt_my_table BY field1 DESCENDING.

Removing Adjacent Duplicates

Once the data is sorted, removing adjacent duplicates becomes more manageable. To accomplish this in ABAP, you use the ‘DELETE ADJACENT DUPLICATES’ statement. This command is designed to remove duplicate entries that appear consecutively after sorting.

Here’s an example of how to use it:

DATA: lt_my_table TYPE TABLE OF my_structure.

” Populate lt_my_table with data and sort it

DELETE ADJACENT DUPLICATES FROM lt_my_table COMPARING field1 field2.

In this case, ‘DELETE ADJACENT DUPLICATES’ will scan through ‘lt_my_table’ and remove consecutive duplicate entries based on ‘field1’ and ‘field2’. This operation ensures that each entry is unique in the context of the specified fields.

Complete Example

To illustrate the full process of sorting and removing adjacent duplicates here is a complete example:

DATA: lt_my_table TYPE TABLE OF my_structure,

      ls_my_data  TYPE my_structure.

” Sample data population

ls_my_data-field1 = ‘Value1’.

ls_my_data-field2 = ‘ValueA’.

APPEND ls_my_data TO lt_my_table.

ls_my_data-field1 = ‘Value1’.

ls_my_data-field2 = ‘ValueA’.

APPEND ls_my_data TO lt_my_table.

ls_my_data-field1 = ‘Value2’.

ls_my_data-field2 = ‘ValueB’.

APPEND ls_my_data TO lt_my_table.

” Sort the table

SORT lt_my_table BY field1 field2.

Remove adjacent duplicates

DELETE ADJACENT DUPLICATES FROM lt_my_table COMPARING field1 field2.

 

In this example, after sorting ‘lt_my_table’ by ‘field1’ and ‘field2’, the ‘DELETE ADJACENT DUPLICATES’ statement removes any consecutive duplicate entries. This ensures that the internal table contains only unique consecutive entries according to the specified fields.

 

Author : Aniket Pawar, 9373518385                  

24DDIC2707 – Efficient Data Management Sorting and Removing Adjacent Duplicates

In the world of ABAP programming, effectively managing and processing data is key to building robust applications. One common requirement is to sort data and remove adjacent duplicates from internal tables. This post will guide you through the process of sorting data and eliminating adjacent duplicate entries using ABAP’s powerful internal table operations.

Sorting Data

Sorting internal tables in ABAP is straightforward and efficient using the ‘SORT’ statement. This statement arranges the entries of an internal table based on the fields you specify. Sorting is crucial for organizing data and is often a precursor to other operations such as filtering or removing duplicates.

Here’s an example of how to sort an internal table:

DATA: lt_my_table TYPE TABLE OF my_structure.

” Populate lt_my_table with data

SORT lt_my_table BY field1 field2.

In this example, ‘lt_my_table’ is sorted first by ‘field1’ and then by ‘field2’. The ‘SORT’ statement ensures that the data in ‘lt_my_table’ is arranged in ascending order according to these fields. If you need descending order sorting you can use the ‘DESCENDING’ keyword:

SORT lt_my_table BY field1 DESCENDING.

Removing Adjacent Duplicates

Once the data is sorted, removing adjacent duplicates becomes more manageable. To accomplish this in ABAP, you use the ‘DELETE ADJACENT DUPLICATES’ statement. This command is designed to remove duplicate entries that appear consecutively after sorting.

Here’s an example of how to use it:

DATA: lt_my_table TYPE TABLE OF my_structure.

” Populate lt_my_table with data and sort it

DELETE ADJACENT DUPLICATES FROM lt_my_table COMPARING field1 field2.

In this case, ‘DELETE ADJACENT DUPLICATES’ will scan through ‘lt_my_table’ and remove consecutive duplicate entries based on ‘field1’ and ‘field2’. This operation ensures that each entry is unique in the context of the specified fields.

Complete Example

To illustrate the full process of sorting and removing adjacent duplicates here is a complete example:

DATA: lt_my_table TYPE TABLE OF my_structure,

      ls_my_data  TYPE my_structure.

” Sample data population

ls_my_data-field1 = ‘Value1’.

ls_my_data-field2 = ‘ValueA’.

APPEND ls_my_data TO lt_my_table.

ls_my_data-field1 = ‘Value1’.

ls_my_data-field2 = ‘ValueA’.

APPEND ls_my_data TO lt_my_table.

ls_my_data-field1 = ‘Value2’.

ls_my_data-field2 = ‘ValueB’.

APPEND ls_my_data TO lt_my_table.

” Sort the table

SORT lt_my_table BY field1 field2.

Remove adjacent duplicates

DELETE ADJACENT DUPLICATES FROM lt_my_table COMPARING field1 field2.

 

In this example, after sorting ‘lt_my_table’ by ‘field1’ and ‘field2’, the ‘DELETE ADJACENT DUPLICATES’ statement removes any consecutive duplicate entries. This ensures that the internal table contains only unique consecutive entries according to the specified fields.

 

Author : Aniket Pawar, 9373518385