要获得新的aap中的总和小计而不循环

时间:2018-10-10 10:00:44

标签: sap abap

我开始学习新的技巧。但是我有问题。我想在不使用“ LOOP”和“ AT”语句的情况下进行以下结果输出。

我的内部表如下:

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

任何人都可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

如果您的问题是关于如何使用构造函数表达式(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 ) ).

答案 1 :(得分:1)

我按以下方式编写此代码。

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 ) ) ) ).

答案 2 :(得分:0)

如果您不想使用loop at遍历内部表,那么您始终可以使用一个功能模块,该模块将为您遍历内部表并打印总计和小计。

REUSE_ALV_GRID_DISPLAY是这样的功能模块之一。

您可以查看以下链接以获得教程: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