不用循环计算总计和小计?
Calculate total and subtotal without loop?
我开始学习新的 abap。但我有问题。我想在不使用 "LOOP" 和 "AT" 语句的情况下进行如下结果输出。
我有内部 table 喜欢:
Category Amount
AAA 10
AAA 20
BBB 30
CCC 40
CCC 50
CCC 60
我需要将输出显示为:
Category Amount
AAA 10
AAA 20
SUBTOTAL 30
BBB 30
SUBTOTAL 30
CCC 40
CCC 50
CCC 60
SUBTOTAL 150
TOTAL 210
有人可以帮忙吗?
如果您不想使用 loop at 遍历内部 table,那么您始终可以使用一个函数模块来为您遍历内部 table 并打印总计和小计。
REUSE_ALV_GRID_DISPLAY就是这样一个功能模块。
您可以查看以下link教程:http://www.saphub.com/abap-tutorial/abap-alv-total-subtotal/
以上是在过程编程范例中实现的,比在 SAP 中制作报告的原始方式(您将在其中使用打印语句)更新。
但是,比这更新的方法是使用面向对象的 ALV 报告实现。查看 CL_GUI_ALV_GRID。
CL_GUI_ALV_GRID 上有一篇介绍性文章:
https://wiki.scn.sap.com/wiki/display/ABAP/OBJECT+ORIENTED+ALV+Guide
我把这段代码写成下面的样子。
TYPES: LTY_DISPLAY TYPE STANDARD TABLE OF TY_DISPLAY WITH EMPTY KEY.
LT_DISPLAY = REDUCE LTY_DISPLAY
( INIT LIST = VALUE LTY_DISPLAY( )
SUBTOTAL = VALUE LTY_DISPLAY( )
TOTAL = VALUE LTY_DISPLAY( )
LV_TEXT TYPE STRING
FOR GROUPS <GROUP_KEY> OF <WA> IN GT_MAIN GROUP BY ( CATEGORY = <WA>-CATEGORY ) ASCENDING
NEXT lV_TEXT = <GROUP_KEY>
LIST = VALUE LTY_COSP( BASE SUBTOTAL FOR <WA1> IN GROUP <GROUP_KEY> ( <WA1> ) )
SUBTOTAL = VALUE LTY_COSP( BASE LIST ( CATEGORY = 'SUBTOTAL' && LV_TEXT
AMOUNT = REDUCE #( INIT SUM TYPE P
FOR M IN GROUP <GROUP_KEY>
NEXT SUM = SUM + M-AMOUNT ) ) )
TOTAL = VALUE LTY_COSP( BASE SUBTOTAL ( CATEGORY = 'TOTAL'
AMOUNT = REDUCE #( INIT SUM TYPE P
FOR M IN GT_MAIN
NEXT SUM = SUM + M-AMOUNT ) ) ) ).
如果您的问题是关于如何使用构造函数表达式 (ABAP >= 7.40) 构建内部 table(在内存中),而不是将其呈现在屏幕上或假脱机文件中(总计和小计)是功能很好地集成在 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 ) ).
我开始学习新的 abap。但我有问题。我想在不使用 "LOOP" 和 "AT" 语句的情况下进行如下结果输出。
我有内部 table 喜欢:
Category Amount
AAA 10
AAA 20
BBB 30
CCC 40
CCC 50
CCC 60
我需要将输出显示为:
Category Amount
AAA 10
AAA 20
SUBTOTAL 30
BBB 30
SUBTOTAL 30
CCC 40
CCC 50
CCC 60
SUBTOTAL 150
TOTAL 210
有人可以帮忙吗?
如果您不想使用 loop at 遍历内部 table,那么您始终可以使用一个函数模块来为您遍历内部 table 并打印总计和小计。
REUSE_ALV_GRID_DISPLAY就是这样一个功能模块。
您可以查看以下link教程:http://www.saphub.com/abap-tutorial/abap-alv-total-subtotal/
以上是在过程编程范例中实现的,比在 SAP 中制作报告的原始方式(您将在其中使用打印语句)更新。
但是,比这更新的方法是使用面向对象的 ALV 报告实现。查看 CL_GUI_ALV_GRID。
CL_GUI_ALV_GRID 上有一篇介绍性文章: https://wiki.scn.sap.com/wiki/display/ABAP/OBJECT+ORIENTED+ALV+Guide
我把这段代码写成下面的样子。
TYPES: LTY_DISPLAY TYPE STANDARD TABLE OF TY_DISPLAY WITH EMPTY KEY.
LT_DISPLAY = REDUCE LTY_DISPLAY
( INIT LIST = VALUE LTY_DISPLAY( )
SUBTOTAL = VALUE LTY_DISPLAY( )
TOTAL = VALUE LTY_DISPLAY( )
LV_TEXT TYPE STRING
FOR GROUPS <GROUP_KEY> OF <WA> IN GT_MAIN GROUP BY ( CATEGORY = <WA>-CATEGORY ) ASCENDING
NEXT lV_TEXT = <GROUP_KEY>
LIST = VALUE LTY_COSP( BASE SUBTOTAL FOR <WA1> IN GROUP <GROUP_KEY> ( <WA1> ) )
SUBTOTAL = VALUE LTY_COSP( BASE LIST ( CATEGORY = 'SUBTOTAL' && LV_TEXT
AMOUNT = REDUCE #( INIT SUM TYPE P
FOR M IN GROUP <GROUP_KEY>
NEXT SUM = SUM + M-AMOUNT ) ) )
TOTAL = VALUE LTY_COSP( BASE SUBTOTAL ( CATEGORY = 'TOTAL'
AMOUNT = REDUCE #( INIT SUM TYPE P
FOR M IN GT_MAIN
NEXT SUM = SUM + M-AMOUNT ) ) ) ).
如果您的问题是关于如何使用构造函数表达式 (ABAP >= 7.40) 构建内部 table(在内存中),而不是将其呈现在屏幕上或假脱机文件中(总计和小计)是功能很好地集成在 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 ) ).