on 2023 Aug 08 4:59 PM
Hi Expert, ich have a Database which contain Parent and Child Groups..
Some Groups has Child but others not ?
It can be more than 6 Groups linked together with this Child relation, other groups can has on Child group..
How can I replace the following Loop with another one which can be more dynamically ?
SELECT * FROM zgroups INTO TABLE @DATA(lt_groups).
LOOP AT lt_groups ASSIGNING FIELD-SYMBOL(<ls_groups>).
SELECT * FROM zgroups INTO TABLE @DATA(lt_groups_kind) WHERE group_parent = @<ls_groups>-group_id.
LOOP AT lt_groups_kind ASSIGNING FIELD-SYMBOL(<ls_groups_kind>) .
SELECT * FROM zgroups INTO TABLE @DATA(lt_groups_kind_sub) WHERE group_parent = @<ls_groups_kind>-group_id.
LOOP AT lt_groups_kind_sub ASSIGNING FIELD-SYMBOL(<ls_groups_kind_sub>) .
SELECT * FROM zgroups INTO TABLE @DATA(lt_groups_kind_sub_sub) WHERE group_parent = @<ls_groups_kind_sub>-group_id.
* ETC+++++++ loop again
ENDLOOP.
ENDLOOP.
ENDLOOP.
Please, any Idea will be appreciated.
Regards,
Request clarification before answering.
Hi Sandra Home,
With a form or method you can call it recursively supporting many levels automaticaly:
SELECT * FROM zgroups INTO TABLE @DATA(gt_groups).
PERFORM go_down_nodes USING ''.
FORM go_down_nodes USING p_parent.
LOOP AT lt_groups ASSIGNING FIELD-SYMBOL(<ls_groups>) WHERE group_parent = p_parent.
PERFORM go_down_nodes USING <ls_groups>-group_id.
ENDLOOP.
ENDFORM.
Regards, Fernando Da RósYou must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sandra,
The suggestion works fine, but I do not know what you want from your code.... below a sample just writing and the result.
TYPES:
BEGIN OF ty_dummy_hier,
group_id TYPE char20,
group_parent TYPE char20,
END OF ty_dummy_hier.
DATA gt_groups TYPE STANDARD TABLE OF ty_dummy_hier.
APPEND VALUE #( group_id = '01' group_parent = '' ) TO gt_groups.
APPEND VALUE #( group_id = '01.01' group_parent = '01' ) TO gt_groups.
APPEND VALUE #( group_id = '01.01.01' group_parent = '01.01' ) TO gt_groups.
APPEND VALUE #( group_id = '01.01.01.01' group_parent = '01.01.01' ) TO gt_groups.
APPEND VALUE #( group_id = '02' group_parent = '' ) TO gt_groups.
APPEND VALUE #( group_id = '02.01' group_parent = '02' ) TO gt_groups.
PERFORM go_down_nodes USING ''.
FORM go_down_nodes USING p_parent.
LOOP AT gt_groups ASSIGNING FIELD-SYMBOL(<fs_groups>) WHERE group_parent = p_parent.
WRITE: / <fs_groups>-group_id, ' group_parent = ', p_parent.
PERFORM go_down_nodes USING <fs_groups>-group_id.
ENDLOOP.
ENDFORM.
Regards, Fernando Da Rós
Try something like that:
LOOP AT lt_groups ASSIGNING FIELD-SYMBOL(<fs_group>) GROUP BY <fs_group>-group_id.
* Group by group_id
LOOP AT lt_groups ASSIGNING FIELD-SYMBOL(<fs_group_children>)
WHERE group_parent = <fs_group>-group_id
GROUP BY <fs_group_children>-group_parent.
* Group by group_parent for all children
LOOP AT GROUP <fs_group_children> ASSIGNING FIELD-SYMBOL(<fs_group_children_line>).
* This will loop at all children of that group
ENDLOOP.
ENDLOOP. "LOOP AT lt_groups ASSIGNING FIELD-SYMBOL(<fs_group_children>)
ENDLOOP. "LOOP AT lt_groups ASSIGNING FIELD-SYMBOL(<fs_group>) GROUP BY <fs_group>-group_id.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thank you for sharing
You might want to have a look at the AT NEW /ENDAT etc. language constructs to be used within LOOP/ENDLOOP.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Or the rather more modern GROUP BY
User | Count |
---|---|
75 | |
30 | |
9 | |
8 | |
7 | |
6 | |
6 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.