cancel
Showing results for 
Search instead for 
Did you mean: 

complex challenged Loop on internal table with

JanaSharb
Explorer
0 Kudos

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,

Sandra_Rossi
Active Contributor

The question would be easier to understand if you explain how your ZGROUPS table works with an example, and which hierarchy this example corresponds to.

Thanks to Fernando, that would be:

zgroups = VALUE #(
         ( group_id = '01'          group_parent = '' )
         ( group_id = '01.01'       group_parent = '01' )
         ( group_id = '01.01.01'    group_parent = '01.01' )
         ( group_id = '01.01.01.01' group_parent = '01.01.01' )
         ( group_id = '02'          group_parent = '' )
         ( group_id = '02.01'       group_parent = '02' ) ).
which corresponds to this hierarchy
├── 01
│   └── 01
│       └── 01
│           └── 01
└── 02
└── 01

Accepted Solutions (1)

Accepted Solutions (1)

fedaros
Advisor
Advisor

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ós
JanaSharb
Explorer
0 Kudos

I will try it.. Thank you

JanaSharb
Explorer
0 Kudos

the old parent will be replaced through the new although it still has child to process

fedaros
Advisor
Advisor
0 Kudos

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

Answers (2)

Answers (2)

thkolz
Contributor

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.
JanaSharb
Explorer

thank you for sharing

manfred_reinart
Employee
Employee
0 Kudos

You might want to have a look at the AT NEW /ENDAT etc. language constructs to be used within LOOP/ENDLOOP.

matt
Active Contributor

Or the rather more modern GROUP BY