如果您的问题是关于如何使用构造函数表达式 (ABAP >= 7.40) 构建内部表(在内存中),而不是在屏幕上或在假脱机文件中呈现它(总计和小计是 ALV 中很好集成的功能并且易于使用),那么这是一种方法(这里使用 ASSERT 来表明最终值符合预期):
TYPES : BEGIN OF ty_line,
category TYPE string,
amount TYPE decfloat16,
END OF ty_line,
ty_lines TYPE STANDARD TABLE OF ty_line WITH DEFAULT KEY.
DATA(gt_main) = VALUE ty_lines( ( category = 'AAA' amount = 10 )
( category = 'AAA' amount = 20 )
( category = 'BBB' amount = 30 )
( category = 'CCC' amount = 40 )
( category = 'CCC' amount = 50 )
( category = 'CCC' amount = 60 ) ).
DATA(lt_display) = VALUE ty_lines(
( LINES OF VALUE #(
FOR GROUPS <g> OF <line> IN gt_main
GROUP BY ( category = <line>-category )
( LINES OF VALUE #( FOR <line2> IN GROUP <g> ( <line2> ) ) )
( category = 'SUBTOTAL'
amount = REDUCE #( INIT subtotal TYPE ty_line-amount
FOR <line2> IN GROUP <g>
NEXT subtotal = subtotal + <line2>-amount ) ) ) )
( category = 'TOTAL'
amount = REDUCE #( INIT total TYPE ty_line-amount
FOR <line> IN gt_main
NEXT total = total + <line>-amount ) ) ).
ASSERT lt_display = VALUE ty_lines( ( category = 'AAA' amount = 10 )
( category = 'AAA' amount = 20 )
( category = 'SUBTOTAL' amount = 30 )
( category = 'BBB' amount = 30 )
( category = 'SUBTOTAL' amount = 30 )
( category = 'CCC' amount = 40 )
( category = 'CCC' amount = 50 )
( category = 'CCC' amount = 60 )
( category = 'SUBTOTAL' amount = 150 )
( category = 'TOTAL' amount = 210 ) ).