确定 LOOP AT ... GROUP BY 中的最后一组?

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

我正在寻找与 LOOP AT ... GROUP BY 语句中的 AT LAST 语句等效的语句。

我正在使用 group by 循环,出于性能原因,当一定数量的记录已累积到内部 table 时,我每隔几组执行一次调用方法。但是我想在循环结束时为 运行 这个调用添加一个额外的条件,如果我在最后一组。

下面我写了一段executable代码来模拟我的问题,我想在退出循环之前处理我最后的material。

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.

你是这个意思吗?
sy-tabix 当前循环索引可能对这里有帮助。 否则您可能需要先计算组数。

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 lines( materials ) = sy-tabix.
      "me->process_boms(materials_bom).
      clear materials_bom.
    endif.
endloop.

首先:最好是 real-world examples,而不是像您那样只是无意义的 table 充满随机数字嗡嗡声。这样你可能会得到错误的答案。

如果你只想处理最后一组,你可以使用GROUP INDEX

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.

注意: 必须对 table 进行排序,以便 INDEX 显示正确的值。

在此代码段中使用了非常简单的方法:首先我们计算唯一组的总数,然后检查每个组索引是否是最后一个。

请注意,数据集中的 last 仅取决于排序顺序,因此您可能很容易得到不符合您要求的值。