- SAP ABAP
- What is SAP ABAP
- SAP ABAP Data Dictionary and Domain
- SAP ABAP Data Element
- SAP ABAP Database Table
- SAP ABAP Database tables and views
- SAP ABAP Foreign Key
- SAP ABAP Indexes
- SAP ABAP Structure
- SAP ABAP Package
- SAP ABAP Adding Fields to SAP Standard Table
- SAP ABAP Internal Table and Database Table
- SAP ABAP Select Option and Parameter
- SAP ABAP Types of Internal Table
- SAP ABAP ways of Declaring Internal Tables
- SAP ABAP Mastering Initialization Technique
- SAP ABAP Operations on Internal Table
- SAP ABAP Record Retrieval
- SAP ABAP Insert, Modify and Delete data in the Internal table by using Keywords
- SAP ABAP Sorting and Removing Adjacent Duplicates
- SAP ABAP Seamless Data Transfer Between Internal Tables
- SAP ABAP Search Help Types
- SAP ABAP Lock Objects and Types
- SAP ABAP Buffering and Its Types
- SAP ABAP TMG
- SAP ABAP Table Types
- SAP ABAP Views
- SAP ABAP Control Break Statement
- SAP ABAP COMMIT and ROLLBACK
- SAP ABAP Joins
- SAP For All Entries
- SAP ABAP Procedure to Fill Final Internal Table
- SAP ABAP Modularization
- SAP ABAP Function Group and Function Module
- SAP ABAP SELECT Options
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