Grouping in a loop ABAP - GROUP BY

In version 7.40 in ABAP has the ability to group values in a loop and process these groups as needed. This add-on not only reduces the number of lines of code for implementing tasks, but also improves performance.

In this article, as an example of a simple task of summing values and highlighting subtotals, we will analyze the use of the supplement LOOP GROUP BY. The task is to group:

  • Sales organizations and factories

  • According to the materials

  • And we also had a requirement to display detailed records without grouping, so it was impossible to organize grouping earlier, for example, at the database level.

On our channel in See a detailed discussion of this and other tasks. Subscribe and leave your comments:

.
Hover over any line with a number 
and you will see a pop-up comment!

 LOOPáATált_dataáINTOáDATA(ls_data) "1"
  GROUPáBYá( vkorgá=áls_data-vkorgáwerksá=áls_data-werksá) "2"
  REFERENCEáINTOáDATA(group_ref_werks). "3"
  
  CLEARáls_grp_werks.
  LOOPáATáGROUPágroup_ref_werksáINTOáDATA(ls_werks) "4"
    GROUPáBYá(ámatnrá=áls_werks-matnrá) "5"
áá  REFERENCEáINTOáDATA(group_ref). "6"

    LOOPáATáGROUPágroup_refáREFERENCEáINTOáDATA(lr_tab)."7"
      IFáls_matnráISáINITIAL. "8"
        MOVE-CORRESPONDINGálr_tab->*áTOáls_matnr.
      ELSE. 
        ls_matnr-mengeá=áls_matnr-mengeá+álr_tab->menge.
      ENDIF.
    ENDLOOP. "9"

    ls_matnr-menge_frá=áls_matnr-menge_toá-áls_matnr-menge_r."10"
    CLEARáls_val.
    MOVE-CORRESPONDINGáls_matnráTOáls_val."11"
    ls_class-mengeá=áls_grp_class-mengeá+áls_matnr-menge."12"
  ENDLOOP."13"
  CLEARáls_val.
  MOVE-CORRESPONDINGáls_grp_classáTOáls_val.
  APPEND ls_val TO lt_result."14"
ENDLOOP.
Assistance to the project on the domain and hosting. Thanks!

"1" - DATA (ls_data) - dynamically determine the structure.
"2" - Set the fields by which we want to group the records of the vkorg and werks tables.
"3" - group_ref_werks - Dynamically define a variable in which groups of records with the same key values vkorg and werks will be placed in turn.
"4" - Using the LOOP AT GROUP construct, we now run through the selected group of records group_ref_werks with the same values vkorg and werks.
"5" - At this step, we could already process the data and not group, but we need additional grouping according to the material.
"6" - And now we put in this variable a group with the same values not only vkorg and werks but also the same matnr values.
"7" - At the same step, we are already processing the group obtained above and summarizing the values of all records in it.
"8" - If this is the first record from the group, then simply copy the values.
"9" - Exiting this cycle, we processed the entire first group with the same set of vkorg and werks and matnr.
"10" - After that we can calculate the fields dependent on the summed fields.
"11" - After which we save the received record of the amount in the structure of the result for future use.
"12" - Next, we summarize the values for an external cycle that passes through unique values only vkorg and werks.
"13" - After exiting the cycle, we also process the fields of sums by vkorg and werks as necessary.
"14" - And save the data for this passage of the cycle in the final table.