Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

abap - Identify last group in a LOOP AT ... GROUP BY?

I'm looking for the equivalent of the AT LAST statement within the LOOP AT ... GROUP BY statement.

I'm looping using group by, and for performance reasons, I execute a call method every few groups, when a certain amount of records has been accumulated into an internal table. But I would like to add an extra condition to run this call at the end of the loop, if I'm in the last group.

Below I wrote a piece of executable code simulating my problem, I would like to process my last material before exiting the loop.

constants: processing_size_threshold type i value 10.
types: begin of ty_material,
         material_num type c length 3,
       end of ty_material.
data:  materials type standard table of ty_material,
       materials_bom type standard table of ty_material.

materials = value #( for i = 1 then i + 1 while i <= 3
                     ( material_num = conv #( i ) ) ).


loop at materials reference into data(material_grp) group by material_grp->material_num.

    loop at group material_grp reference into data(material).
      materials_bom = value #( base materials_bom (
                               lines of value #( for j = 1 then j + 1 while j <= 5
                                               ( material_num = |{ material->material_num }{ j }| ) ) ) ).
    endloop.

   "The 2nd condition of this IF is what I'm not able to figure out, I need material 3 BOM to be processed here
    if lines( materials_bom ) >= processing_size_threshold. "or group_num = last_group.
      "me->process_boms(materials_bom).
      clear materials_bom.
    endif.
endloop.
question from:https://stackoverflow.com/questions/65846496/identify-last-group-in-a-loop-at-group-by

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First: it is better to put real-world examples than just nonsensical tables filled with random numeric buzz like you did. You may just get the wrong answers that way.

If you want to process just the last group, you can use the GROUP INDEX for this:

SELECT matnr AS matno
  FROM vbap UP TO 100 ROWS
  INTO TABLE @DATA(materials)
  ORDER BY matnr.

TYPES: ty_mats LIKE materials.
DATA: bom LIKE materials.

DATA(uniques) = lines( VALUE ty_mats( FOR GROUPS value OF <line> IN materials GROUP BY ( matno = <line>-matno ) WITHOUT MEMBERS ( value ) ) ).
LOOP AT materials REFERENCE INTO DATA(refmat) GROUP BY ( id = refmat->matno size = GROUP SIZE gi = GROUP INDEX ) ASCENDING REFERENCE INTO DATA(group_ref).
  CHECK group_ref->*-gi = uniques.
  bom = VALUE ty_mats( BASE bom FOR <mat> IN GROUP group_ref ( matno = <mat>-matno ) ).
  me->process_boms( bom ). "<--here comes the drums
ENDLOOP.

Note: the table must be sorted for INDEX to show correct values.

In this snippet very simple approach is utilized: first we calculate the total number of unique groups and then check each group index whether it is the last one.

Note, that what is last in your dataset depends only on sort order, so you may easily end up with the inaccurate values for your requirement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...