To get total and subtotal without loop in new abap

余生颓废 提交于 2020-07-22 09:20:26

问题


I am starting learning the new abap. But i have problems. I want to make result output as below without using "LOOP" and "AT" statements.

I have internal table like:

Category Amount
    AAA     10
    AAA     20
    BBB     30
    CCC     40
    CCC     50
    CCC     60

I need to display output as:

Category Amount
    AAA       10
    AAA       20
    SUBTOTAL  30
    BBB       30
    SUBTOTAL  30
    CCC       40
    CCC       50
    CCC       60
    SUBTOTAL  150
    TOTAL     210

Can anyone help with this?


回答1:


If your question is about how to build an internal table (in memory) with constructor expressions (ABAP >= 7.40), rather than rendering it on the screen or in a spool file (totals and subtotals are features well integrated in ALV and easy to use), then here's one way to do it (ASSERT is here to show that the final value is as expected) :

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



回答2:


I make this code as below.

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



回答3:


If you don't want to loop through the internal table using loop at, then you can always use a function module that will go through the internal table for you and print the totals and subtotals.

REUSE_ALV_GRID_DISPLAY is one such function module.

You can check the following link for a tutorial: http://www.saphub.com/abap-tutorial/abap-alv-total-subtotal/

The above is implemented in a procedural programming paradigm and is newer than original way of making reports in SAP (where you would use print statements).

However, an even newer way than that would to use the object-oriented implementation of ALV reports. Look into CL_GUI_ALV_GRID.

Here is an introductory article on CL_GUI_ALV_GRID: https://wiki.scn.sap.com/wiki/display/ABAP/OBJECT+ORIENTED+ALV+Guide



来源:https://stackoverflow.com/questions/52737581/to-get-total-and-subtotal-without-loop-in-new-abap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!