banner ads

Control Break with SUM statement

SUM statement can only be used in LOOP – ENDLOOP statement. It is considered in AT – END AT control break structure. Inside the control break SUM calculates the sum of all the fields which are like I or P or F type. SUM holds the summation at it is reflected to the respective work areas. This summation must be the total of current control break. SUM doesn’t work when the row type of the internal table contains table type components.

Here we have prepared an example in which purchase order item table EKPO has been selected. Now we use SUM statement which calculates the summation of quantity & net price inside the AT END OF control statement. Hence at the last occurrence of PO the system calculates the total quantity & price for different item level.


*-------Declaring tables for select option-----------------------------*
TABLES: ekpo.

*------Declaring local structure for work area & table-----------------*
        ebeln TYPE ekpo-ebeln,
        ebelp TYPE ekpo-ebelp,
        menge TYPE ekpo-menge,
        meins TYPE ekpo-meins,
        netpr TYPE ekpo-netpr,
       END OF ty_ekpo.

*-----Declaration of work area & internal table------------------------*
DATA: wa_ekpo TYPE ty_ekpo,
      it_ekpo TYPE TABLE OF ty_ekpo,
      v_flag  TYPE c.

*-----Event Initialization---------------------------------------------*
  SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln.

*-----Event Start of Selection-----------------------------------------*
  SELECT ebeln ebelp menge meins netpr
    FROM ekpo INTO TABLE it_ekpo
    WHERE ebeln IN s_ebeln.

  IF sy-subrc = 0.

    "Table needs to be sorted.
    "Otherwise AT NEW & AT END OF will operate data wrongly
    SORT it_ekpo.
    LOOP AT it_ekpo INTO wa_ekpo.

      "Triggers at the first loop iteration only
      AT FIRST.
        WRITE:    'Purchase Order' COLOR 3,
               20 'Item' COLOR 3,
               35 'Quantity' COLOR 3,
               45 'Unit' COLOR 3,
               54 'Net Price' COLOR 3.

      "Triggers when new PO will come into the loop
      AT NEW ebeln.
        v_flag = 'X'.

      IF v_flag = 'X'.
        WRITE:   / wa_ekpo-ebeln,
                20 wa_ekpo-ebelp,
                27 wa_ekpo-menge,
                45 wa_ekpo-meins,
                50 wa_ekpo-netpr.
        WRITE: /20 wa_ekpo-ebelp,
                27 wa_ekpo-menge,
                45 wa_ekpo-meins,
                50 wa_ekpo-netpr.

      "Triggers at the last occurrence of PO in the loop
      AT END OF ebeln.
        WRITE: /27 '=================',
                50 '=============='.
        SUM"SUM adds & holds all the I/P/F data
        "Here it holds for this control range

        WRITE: / 'Sub Total: ' COLOR 5,
                27 wa_ekpo-menge,
                50 wa_ekpo-netpr.

      "Triggers at the last loop iteration only
      AT LAST.
        SUM"SUM adds & holds all the I/P/F data
        "Here it holds the total of loop range

        WRITE: / 'Quantity & Net Price' COLOR 4,
               / 'Grand Total: ' COLOR 4,
               27 wa_ekpo-menge,
               50 wa_ekpo-netpr.
      CLEAR: wa_ekpo, v_flag.

Now at the debugging level we see the behavior of SUM statement. At SY-TABIX = 4 one PO holds its last occurrence and the system calculates the total quantity & price by using the SUM statement. Here the total will be considered for this particular control only.

We can use SUM in AT LAST control break as well. In this case it will calculate the all total of quantity & price. Since AT LAST triggers at the last loop iteration, the SUM considers the total quantity & price of all different POs.

Below is the output.

No comments