Application Development and Automation 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: 
Read only

Question with LOOP AT itab

Former Member
0 Likes
1,828

The documentation says that itab is a functional Operator so i replaced it with a functional method. Normal Loop works fine but GROUP BY gets error Messages "no Group binding" . If i insert a helper variable for the functional method result the code works.

Why?

I put some Example Coding in the attachment (modified SAP demo Report)

1 ACCEPTED SOLUTION
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
1,683

Hello,

There was indeed a gap in the documentation in Release 7.40 that was closed in Release 7.50.

See LOOP AT GROUP.

"This loop is only possible within a LOOP across an internal table with the addition GROUP BY,

  • for which the internal table itab is specified directly as a data object and not specified as the result of a call or expression "

The reason for this is neatly explained by Suhas

Horst

7 REPLIES 7
Read only

Jelena_Perfiljeva
Active Contributor
0 Likes
1,683

Say what? Is this an ABAP question? We have an operator called "itab" now? In which release? What documentation did you read?

Sorry, I ain't opening any zip files to check the code examples. If you have a specific question then kindly find a specific [small] fragment in your code it pertains to and post it directly in the question.

Read only

0 Likes
1,683

Sorry misspelled

I put in the correct citation:

"Executes a table iteration as a loop across an internal table itab. itab is a functional operand position."

Unfortunately I only have an old Version of Internet Explorer and the post editing Features of this Editor are not functioning very well. I can't really insert any code examples but I will try for my next posting.

Read only

Sandra_Rossi
Active Contributor
0 Likes
1,683

At first glance, I had the same reaction as Jelena, but I opened the zip files and copied the code to a test program, and I must say I didn't understand, maybe it's a bug and you could post a message to SAP, and I'm now waiting for answers from experts.

Anyway, I agree with Jelena, and your question is worth re-posting with a minimalist code (inside post, not inside zip) so that you touch more people, and it will be easier to understand it. See example posts by Suhas Saha (for instance: http://scn.sap.com/thread/3881351 )

Read only

Former Member
0 Likes
1,683

The SAP Version used was NW 7.40 SP 13

This editor really is browser dependant:

Testreport:


REPORT  zflpi_test.

CLASS demo DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS:

      main.

  PRIVATE SECTION.

    TYPES:

      BEGIN OF line,

        key TYPE i,

        num TYPE i,

      END OF line,

      itab TYPE STANDARD TABLE OF line WITH DEFAULT KEY.

ENDCLASS.

CLASS demo IMPLEMENTATION.

  METHOD main.

    DATA(out) = cl_demo_output=>new( ).

    DATA:

      BEGIN OF aggregate,

        sum TYPE i,

        max TYPE i,

        min TYPE i,

        avg TYPE decfloat34,

      END OF aggregate.

  "wont work with group by

  "loop at zcl_flpi_demo=>create( )->get_data( ) assinging field-symbol(<wa>)

  "works fine

    data(numbers) = zcl_flpi_demo=>create( )->get_data( ).

    LOOP AT numbers ASSIGNING FIELD-SYMBOL(<wa>)

         GROUP BY ( key = <wa>-key  count = GROUP SIZE )

         ASCENDING

         ASSIGNING FIELD-SYMBOL(<group_key>).

      out->next_section( |Group Key: { <group_key>-key }| ).

      DATA(members) = VALUE itab( FOR m IN GROUP <group_key> ( m ) ).

      aggregate-sum = REDUCE i( INIT sum = 0

                                FOR m IN GROUP <group_key>

                                NEXT sum = sum + m-num ).

      aggregate-max = REDUCE i( INIT max = 0

                                FOR m IN GROUP <group_key>

                                NEXT max = nmax( val1 = max

                                                 val2 = m-num ) ).

      aggregate-min = REDUCE i( INIT min = 101

                                FOR m IN GROUP <group_key>

                                NEXT min = nmin( val1 = min

                                                 val2 = m-num ) ).

      aggregate-avg = aggregate-sum / <group_key>-count.

      SORT members BY num DESCENDING.

      out->write( members

        )->write( aggregate ).

    ENDLOOP.

    out->display( ).

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  demo=>main( ).

And the global class:


CLASS zcl_flpi_demo DEFINITION

  PUBLIC

  FINAL

  CREATE PUBLIC .

  PUBLIC SECTION.

    TYPES:

      BEGIN OF line,

        key TYPE i,

        num TYPE i,

      END OF line,

      itab TYPE STANDARD TABLE OF line WITH DEFAULT KEY.

    CLASS-METHODS create

      RETURNING

        value(r_result) TYPE REF TO zcl_flpi_demo.

    METHODS get_data RETURNING VALUE(result) type itab.

  PROTECTED SECTION.

  PRIVATE SECTION.

ENDCLASS.

CLASS zcl_flpi_demo IMPLEMENTATION.

  METHOD create.

    CREATE OBJECT r_result.

  ENDMETHOD.

  METHOD get_data.

    DATA(keys) = 3.

    DATA(lines) = 10.

    DATA(rnd_key) = cl_abap_random_int=>create(

      seed = CONV i( sy-uzeit ) min = 1 max = keys ).

    DATA(rnd_num) = cl_abap_random_int=>create(

      seed =  sy-uzeit + 1  min = 1 max = 100 ).

    result = VALUE #( FOR j = 1 UNTIL j > lines

                       ( key = rnd_key->get_next( )

                         num = rnd_num->get_next( ) ) ).

  ENDMETHOD.

ENDCLASS.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,683

Nice question. Seems to me like an undocumented feature 

Anyway this statement has me wondering (ABAP Keyword Documentation)


If the internal table is specified as the return value or result of a functional method, a constructor expression, or a table expression, the value is persisted for the duration of the loop. Afterwards, it is no longer possible to access the internal table.

Because with GROUP BY addition the LOOP is processed in 2 steps -

  1. Grouping
  2. Loop at Group

I guess the internal table is not accessible in the second step and therefore you get the error.

Calling in to pitch in his views

BR,

Suhas

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
1,684

Hello,

There was indeed a gap in the documentation in Release 7.40 that was closed in Release 7.50.

See LOOP AT GROUP.

"This loop is only possible within a LOOP across an internal table with the addition GROUP BY,

  • for which the internal table itab is specified directly as a data object and not specified as the result of a call or expression "

The reason for this is neatly explained by Suhas

Horst

Read only

0 Likes
1,683

Hello Horst and Suhas - the argument is not really conceivable.

OK, the internal table that resulted from a functional expression call is an anonymous data object. But if it is internally possible to keep this data object for one processing step (as in a normal loop) - why shouldn't it also be internally possible to keep it for a second processing step (as in the GROUP BY loop)

So let's keep the answer simple: it's not implemented.

Regards, Rüdiger