Advertisement
rplantiko

SELECT DISTINCT for internal tables

Jun 3rd, 2016
1,370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 3.28 KB | None | 0 0
  1. report zz_extract_subtable.
  2.  
  3. types: ernam_tab type standard table of ernam with non-unique default key,
  4.        vbak_tab  type standard table of vbak with non-unique key vbeln.
  5.  
  6. * Extract a standard table of elementary line type ERNAM
  7. * from a table of some 100 VBAK rows
  8. * containing the distinct values that appear in column VBAK-ERNAM
  9.  
  10. * Different ways to do it, with old and with new syntax.
  11.  
  12. start-of-selection.
  13.   perform start.
  14.  
  15. * ---
  16. form start.
  17.  
  18.   data: lt_vbak  type vbak_tab,
  19.         lt_ernam type ernam_tab.
  20.  
  21.   select * up to 100 rows from vbak into table lt_vbak.
  22.  
  23.   perform with_collect using lt_vbak changing lt_ernam.
  24.   perform add_result using `with collect` lt_ernam.
  25.  
  26.   perform with_explicit_aux_hash using lt_vbak changing lt_ernam.
  27.   perform add_result using `with auxiliary hash` lt_ernam.
  28.  
  29.   perform with_value_for using lt_vbak changing lt_ernam.
  30.   perform add_result using `with value for` lt_ernam.
  31.  
  32.   perform with_reduce_for using lt_vbak changing lt_ernam.
  33.   perform add_result using `with reduce for` lt_ernam.
  34.  
  35.   perform with_grouping using lt_vbak changing lt_ernam.
  36.   perform add_result using `with grouping` lt_ernam.
  37.  
  38.   cl_demo_output=>display(  ).
  39.  
  40. endform.
  41.  
  42. form with_collect using it_vbak type vbak_tab
  43.                   changing ct_ernam type ernam_tab.
  44.   clear ct_ernam.
  45.   loop at it_vbak assigning field-symbol(<ls_vbak>).
  46.     collect <ls_vbak>-ernam into ct_ernam.
  47.   endloop.
  48.  
  49. endform.
  50.  
  51. form with_explicit_aux_hash using it_vbak type vbak_tab
  52.                             changing ct_ernam type ernam_tab.
  53.  
  54.   data: lt_ernam type hashed table of ernam with unique key table_line.
  55.  
  56.   loop at it_vbak assigning field-symbol(<ls_vbak>).
  57.     insert <ls_vbak>-ernam into table lt_ernam.
  58.   endloop.
  59.   ct_ernam = lt_ernam.
  60.  
  61. endform.
  62.  
  63. form with_value_for using it_vbak type vbak_tab
  64.                     changing ct_ernam type ernam_tab.
  65.  
  66.   ct_ernam = value #( for <ls_vbak> in it_vbak
  67.                       ( <ls_vbak>-ernam )  " Inserting into a hash table will dump on multiple values.
  68.                                            " No way to avoid this
  69.                      ).
  70.   sort ct_ernam.
  71.   delete adjacent duplicates from ct_ernam.
  72.  
  73. endform.
  74.  
  75. form with_reduce_for using it_vbak type vbak_tab
  76.                      changing ct_ernam type ernam_tab.
  77.  
  78.   ct_ernam = reduce #( init names type ernam_tab
  79.                        for <ls_vbak> in it_vbak
  80.                        next names =
  81.                          cond #(
  82.                            when line_exists( names[ table_line = <ls_vbak>-ernam ] ) then names
  83.                            else value #( base names ( <ls_vbak>-ernam ) )
  84.                            )
  85.                        ).
  86.  
  87. endform.
  88.  
  89. form with_grouping using it_vbak type vbak_tab
  90.                    changing ct_ernam type ernam_tab.
  91.  
  92.     clear ct_ernam.
  93.     loop at it_vbak assigning field-symbol(<ls_vbak>)
  94.                     group by <ls_vbak>-ernam
  95.                     ascending
  96.                     assigning field-symbol(<lv_group>).
  97.       append <lv_group> to ct_ernam.
  98.     endloop.
  99.  
  100. endform.
  101.  
  102.  
  103. form add_result using iv_name type string
  104.                       it_ernam type ernam_tab.
  105.   cl_demo_output=>write_data( name = iv_name value = concat_lines_of( table = it_ernam sep = ',' ) ).
  106. endform.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement