abap - Extracting unique values from an internal table -


what efficient way extract unique values column or multiple columns of internal table?

prior abap 7.40's sp08 release, efficient way of extracting unique values internal table or itab following:

loop @ lt_itab assigning <ls_itab>.     append <ls_itab>-value lt_values. endloop. sort lt_values. delete adjacent duplicates lt_values. 

checking presence of given <ls_itab>-value before adding internal table way of guaranteeing uniqueness more computationally expensive when inserting standard table. sorted or hashed destination tables, use:

loop @ lt_itab assigning <ls_itab>.     read table lt_sorted_values key table_line = <ls_itab>-value binary search.     if sy-subrc <> 0.         append <ls_itab>-value lt_sorted_values.     endif. endloop. 

note using first method inserting values dummy table followed append lines of lt_dummy lt_sorted_values may faster, size of intermediate tables can muddle that.


as of abap 7.40 support package 08 however, the group by loops offer better way extract unique values. name indicates these function sql's group by. instance, following code extract unique project numbers internal table:

loop @ lt_project_data assigning field-symbol(<ls_grp_proj>)     group ( project = <ls_grp_proj>-proj_number ) ascending     without members     assigning field-symbol(<ls_grp_unique_proj>).         append <ls_grp_unique_proj>-project lt_unique_projects. endloop. 

the same logic can extended retrieve unique pairs, such composite primary keys of ekpo table, ebeln ("purchasing document", po_nr) , ebelp ("item number of purchasing document", po_item):

loop @ lt_purchasing_document_items assigning field-symbol(<ls_grp_po>)     group ( number = <ls_grp_po>-po_nr                item   = <ls_grp_po>-po_item ) ascending     without members     assigning field-symbol(<ls_grp_po_item>).         append value #( ebeln = <ls_grp_po_item>-number                         ebelp = <ls_grp_po_item>-item ) lt_unique_po_items. endloop. 

according horst keller, 1 of sap designers of new abap 7.40 release, the performance of group loops same manual implementation of these loops. depending on how (in)efficiently such custom loop implemented may faster. note these faster 2 methods given above systems group by loops not available.


note in cases querying database return distinct values faster , performance-wise doing blow abap code uses internal tables out of water, especially on hana systems.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -