I am trying to loop by grouping data with dynamic group parameter.
We can use dynamic queries on WHERE conditions on loops but I do not know whether it is possible to use a dynamic string in group condition.
Here is the sample where user decides by which field to group and then put additional logic based on the decision:
DATA query TYPE string.
IF i_condition_type = 'ERNAM'.
query = |ERNAM = MARA-ERNAM|.
ELSE.
query = |ERSDA = MARA-ERSDA|.
ENDIF.
LOOP AT lt_mara INTO DATA(mara) GROUP BY ( (query) ) "syntax error
ASSIGNING FIELD-SYMBOL(<group>).
LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<line_data>).
"//do something
ENDLOOP.
ENDLOOP.
Is there any way to do this? I am also open to other ideas besides grouping because if I can't dynamically group, I will copy so many lines and change only the group key.
GROUP BY
dynamically which don't exactly work with strings as conditions but can do a lot of things you would usually use dynamic conditions for. If you are interested, check out my answer. – Sperrylite