Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

LOOP AT GROUP BY external loop variable/field symbol

AlbertoR
Participant

Dear all,

A question which I need to raise in order to just clear up my ideas, since I am already achieving what I need using the powerful LOOP AT ... GROUP BY.

I have the following code:

      LOOP AT internal_table INTO DATA(record)
        GROUP BY ( criteria = record-somefield )
        ASSIGNING FIELD-SYMBOL(<group>).
            "some code here
        LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<group_member>).
           "other code: here we can modify the group member using <group_member>
        ENDLOOP.
      ENDLOOP.

I cannot understand the reason to declare the variable "record", in the external part of the loop: it stays initial for the whole time.

I checked several blogs and documentation, so I ask in case somebody knows.

Thanks a lot in advance for your precious help!

1 ACCEPTED SOLUTION

AndreaUS
Product and Topic Expert
Product and Topic Expert

The declaration is used for grouping. In GROUP BY, the line to be examined should be usable in expressions of any complexity, e.g. method call.

This is not possible without binding to a symbol.

If a group symbol is defined, the variable is meaningless after grouping and becomes unbound.

If no group symbol is defined, it takes a representative of the group in each iteration.

Documented in the ABAP Keyword Documentation here.

7 REPLIES 7

Sandra_Rossi
Active Contributor

I hope SAP experts will answer that question.

I think it's because the old compiler has been reused or almost unchanged, so it's more or less transpiling to ABAP byte code via classic ABAP, it was probably easier to ask the developer to declare that intermediate variable.

nomssi
Active Contributor

At least in representative binding, the variable "record" contains the first line of each group in the loop:

LOOP AT internal_table INTO DATA(record) GROUP BY record-somefield.
… record-somefield ...
ENDLOOP.

Sandra_Rossi
Active Contributor
0 Kudos

easier and more legible maybe, because I personally don't see a better alternative.

AndreaUS
Product and Topic Expert
Product and Topic Expert

The declaration is used for grouping. In GROUP BY, the line to be examined should be usable in expressions of any complexity, e.g. method call.

This is not possible without binding to a symbol.

If a group symbol is defined, the variable is meaningless after grouping and becomes unbound.

If no group symbol is defined, it takes a representative of the group in each iteration.

Documented in the ABAP Keyword Documentation here.

Thank you so much Andrea for taking time to answer and for the excellent answer, which made me understand. 🙂

Here is a little code I tried to test what I understood from you:

REPORT test.
CLASS lcl_report DEFINITION.
  PUBLIC SECTION.
    TYPES BEGIN OF idx_val.
    TYPES idx TYPE i.
    TYPES val TYPE string.
    TYPES END OF idx_val.
    TYPES idx_val_tab TYPE STANDARD TABLE OF idx_val WITH DEFAULT KEY.

    METHODS main.
    METHODS even_odd IMPORTING idx           TYPE i
                     RETURNING VALUE(result) TYPE string.

ENDCLASS.

CLASS lcl_report IMPLEMENTATION.
  METHOD main.
    DATA(table) = VALUE idx_val_tab( ( idx = 1 val = 'one' )
                                     ( idx = 2 val =  'two' )
                                     ( idx = 3 val =  'three' )
                                     ( idx = 4 val =  'four' )
                                     ( idx = 5 val =  'five' )
                                     ( idx = 6 val =  'six' ) ).
    LOOP AT table INTO DATA(record)
      GROUP BY ( index_key = even_odd( idx = record-idx  ) )
      ASSIGNING FIELD-SYMBOL(<group>).
      WRITE: / '<group>-index_key:', <group>-index_key.
      LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<group_member>).
        WRITE: / '<group_member>', <group_member>-idx.
      ENDLOOP.
    ENDLOOP.
  ENDMETHOD.
  METHOD even_odd.
    WRITE: / 'METHOD even_or_zero'.
    WRITE: 'idx: ', idx.
    IF idx MOD 2 EQ 0.
      result = 'even'.
    ELSE.
      result = 'odd'.
    ENDIF.
    WRITE: 'result: ', result.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA(report) = NEW lcl_report( ).
  report->main( ).

horst_keller
Product and Topic Expert
Product and Topic Expert

Sandra_Rossi
Active Contributor
0 Kudos

Maybe I "overengineered" your question 😄

even SAP people don't explain how the syntax was designed (for now)

(if the question was only about the reason why it's initial, Andrea answered)