Skip to Content

Collect with table expressions

I like the declarative ABAP style that the table expressions are offering. however, sometimes I have to switch to imperative style because I don't find a solution for the problem. Here's an example. Is this possible using a for expression?

Jörg

Loop at t into l.
  Collect value #(
    Key = l-key
    Val = l-val )
   Into sum.
Endloop.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Feb 10 at 02:27 PM

    GROUP BY

    You have to compute the aggregate yourself, something like

    LOOP AT t INTO l GROUP BY l-key INTO g.
      APPEND VALUE #( key = g
                      val = REDUCE #( INIT s = 0 FOR i IN GROUP g NEXT s = s + i-val ) ).
    ENDLOOP.

    or

    sum_table = VALUE #( FOR GROUPS g OF l IN t GROUP BY l-key
                     LET total = REDUCE #( INIT s TYPE i FOR i IN GROUP g NEXT s = s + i )
                     IN key = g
                        val = total ).

    JNN

    Add comment
    10|10000 characters needed characters exceeded