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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…