Comment? This is exactly how we do it: use REDUCE to compute any aggregate.

The blog’s own CONVERT( ) method uses this approach (and LET to reuse the result in a condition).

regards,

JNN

]]>Actually, as soon as I posted this I’ve got something working:

```
t_sum = VALUE #(
FOR GROUPS mat OF <fs_mat>
IN t_mat
GROUP BY ( matnr = <fs_mat>-matnr ) ASCENDING
( matnr = mat-matnr
menge = REDUCE menge_d( INIT lv_menge TYPE menge_d
FOR ls_mat IN GROUP mat
NEXT lv_menge = lv_menge + ls_mat-menge ) ) ).
```

It does produce the correct result, but any comments? I like the fact that it is one statement (rather than LOOP … COLLECT … ENDLOOP)

]]>```
TYPES: BEGIN OF ty_mat,
matnr TYPE matnr,
menge TYPE menge_d,
END OF ty_mat,
ty_t_mat TYPE STANDARD TABLE OF ty_mat WITH DEFAULT KEY.
DATA: t_mat TYPE ty_t_mat,
t_sum TYPE ty_t_mat.
* (t_mat is already populated)
t_sum = VALUE #(
FOR GROUPS mat OF <fs_mat>
IN t_mat
GROUP BY ( matnr = <fs_mat>-matnr ) ASCENDING
( matnr = mat-matnr ) ).
```

How do I amend this statement to also include in T_SUM the total quantity of each material?

Obviously I can do this in various ways with the COLLECT statement, but I want to know how to do it this way (irrespective of whether or not it’s the best way!)

The final example in the blog looks like it might be doing something similar.

Thanks!

]]>

]]>

7.4 is still beyond cutting edge for us but I’m sure I’ll come back to this as soon as we get there. And it’s a one more item I can bring up as “look, this is why we need to upgrade”.

Thanks again! Well done.

]]>Hello Sergio,

thanks for the test case, I found the bug: you must add the ASCENDING keyword after GROUP BY clause.

```
LOOP AT return INTO DATA(ls_return)
GROUP BY translate( val = ls_return-type from = `AXEWSI `
to = `0123456` ) ASCENDING.
```

And make sure to use the ` separator for string with trailing space.

JNN

]]>

This is a very nice blog showcasing the usage of GROUP BY. Thanks.

Regarding the message example, I have tryed to implement this but I always get the first entry on the table:

```
data(return) = value bapiret2_tab( ( type = 'I' message = 'Type I' )
( type = 'A' message = 'Type A' )
( type = 'X' message = 'Type X' ) ).
loop at return into data(ls_return)"assigning field-symbol(<return>)
group by translate( val = ls_return-type from = 'AXEWSI ' to = '0123456' ).
r_message = ls_return-message.
return.
endloop.
```

r_message will always have the “Type I” value.

As far as I understand the idea fo the group by and translate is to return the message converning the order on the from right?

In the example we should get the message A first.

Am I missing something?

Thanks again for the excelent blog!

Sérgio Fraga

]]>

I see the bug now, thanks for the report Christian.

while the priority( ) mapping would anything not specified to 5, I missed a mapping for type ‘I’ in the translate( ) mapping. I would need something like

`translate( val = ls_return-type from = `AXEWSI ` to = `0123456` ).`

I guess ‘I’ is larger than any numbers so the behavior is still as expected, but this will be a warning to create an ABAP unit test here.

JNN

]]>

Hm ok. But wouldn’t it give incorrect results in this case?

```
...
DATA(lt_return) = VALUE bapiret2_tab( ( type = 'I' message = 'Type I' )
( type = 'X' message = 'Type X' ) ).
DATA(lv_message) = get_current_message( lt_return ).
...
METHOD get_current_message.
LOOP AT it_return INTO DATA(ls_return)
GROUP BY translate( val = ls_return-type from = `AXEWS ` to = `012345` ).
rv_message = ls_return-message.
RETURN.
ENDLOOP.
ENDMETHOD.
```

Or am I getting something wrong?

]]>Thanks for your feedback Daniel.

How could this *ignore duplicates* option be implemented? Maybe in the FILTER operator, with representative binding as in GROUP BY.