3

我开始学习新的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

有人能帮忙吗?

4

3 回答 3

3

如果您的问题是关于如何使用构造函数表达式 (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 ) ).
于 2018-10-11T20:06:42.583 回答
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 ) ) ) ).
于 2018-10-11T09:37:25.737 回答
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

于 2018-10-10T21:53:01.517 回答