What is the most efficient way to extract the unique values from a column or multiple columns of an internal table?
相关问题
- Generate WSDL web-service objects in SAP
- How to programmatically tell if system is R/3 or S
- replace parameter name show on screen?
- Get table field description programmatically
- After message type e, program doesn't return t
相关文章
- JCO IDOC Server for multiple destinations
- SAP DI Server - How to issue first request?
- Parsing SAP IDOC files
- How to connect to a SAP System using sapjco3 and E
- Different ways to call methods in ABAP
- Export data from SAP to SQL Server [closed]
- sap abap alv report fill gap between documents
- How to prevent OData service call on model change
How about this?
If you have 7.40 SP08 or above you can simply use the inline syntax to populate the target table (no need for LOOP GROUP BY):
This works with any type of the target table.
For an older release use:
The above works with sorted tables as well. Although I do not recommend to use sorted tables for this purpose unless you are really sure that only a few lines will be in the result.
The non-zero
sy-subrc
ofINSERT
is simply ignored. No need to do the key lookup twice (once for existence check, once for insert).If the target must be a STANDARD TABLE and you have an old ABAP stack you can alternatively use
This provides the same behavior as with a sorted table but with a standard table. Whether this is more efficient than SORT / DELETE ADJACENT DUPLICATES depends on the number of duplicate entries in itab. The more duplicate entries exist the faster will be the above solution because it avoids the unnecessary appends to the target table. But on the other side appends are faster than inserts.
Prior to ABAP 7.40's SP08 release, the most efficient way of extracting unique values from an internal table or itab is the following:
Checking the presence of a given
<ls_itab>-value
before adding it to the internal table is another way of guaranteeing uniqueness but will probably be much more computationally expensive when inserting into a standard table. For sorted or hashed destination tables, use:Note that using the first method but inserting the values into a dummy table followed by an
APPEND LINES OF lt_dummy INTO lt_sorted_values
may be faster, but the size of the intermediate tables can muddle that.As of ABAP 7.40 Support Package 08 however, the
GROUP BY
loops offer a better way to extract unique values. As the name indicates these function similarly to SQL'sGROUP BY
. For instance, the following code will extract unique project numbers from an internal table:The same logic can be extended to retrieve unique pairs, such as the composite primary keys of the
EKPO
table,EBELN
("Purchasing Document",po_nr
) andEBELP
("Item Number of Purchasing Document",po_item
):According to Horst Keller, one of the SAP designers of the new ABAP 7.40 release, the performance of GROUP BY loops is likely to be the same as a manual implementation of these LOOPs. Depending on how (in)efficiently such a custom loop is implemented it may even be faster. Note that these will be faster than the two methods given above for systems where the
GROUP BY
loops are not available.Note that in most cases querying the database to return
DISTINCT
values will be much faster and performance-wise doing that will blow any ABAP code that uses internal tables out of the water, especially on HANA systems.