Friday, November 27, 2015

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.


REPORT  z_sum NO STANDARD PAGE HEADING.

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

*------Declaring local structure for work area & table-----------------*
TYPES: BEGIN OF ty_ekpo,
        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---------------------------------------------*
INITIALIZATION.
  SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln.

*-----Event Start of Selection-----------------------------------------*
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.
        ULINE.
      ENDAT.

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

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

      "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.
        SKIP.
      ENDAT.

      "Triggers at the last loop iteration only
      AT LAST.
        ULINE.
        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.
      ENDAT.
      CLEAR: wa_ekpo, v_flag.
    ENDLOOP.
  ENDIF.

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.

18 comments:

Yaso Technosap said...

Nice news and thanks for sharing this here to let me know this updates,please know the recent updates by visiting this link also…because I had gain some info from here also:
SAP Training Tutorials | Sap Online Tutorials

Margarate Ruth said...

Nice Blog...
We offer SAP HR ONLINE TRAINING

Sptech Rao said...
This comment has been removed by the author.
Sptech Rao said...

Nice information.
And here you may get spraotechnologies.com More information.

Ramya k said...

Thanks for the great information in your blog SAP HR Training in Chennai

katrinaDcruz said...

Thanks for sharing Info on abap. Keep posting

Santhosh Kumar said...

Sum syntax is very useful for me. I am in Sap for the 1 year but today only i know this keyword.

Rahul said...

It was very nice blog to learn about SAP HR.Thanks for sharing new things.SAP HR

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

Shalini said...


Hi, This is shalini from Chennai learned SAP Training in Chennai from mr.karthick. The training really was good and i got selected in leading mnc company as SAP Consultant.

You can contact 8122241286 for Best SAP Training in Chennai or reach www.thecreatingexperts.com

Shruti Ramalingam said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

sanayamohit said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

sushma said...

Best SAP Success Factor Training in Chennai

http://thecreatingexperts.com/sap-training-in-chennai/
http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
http://thecreatingexperts.com/sap-mm-training-in-chennai/
http://thecreatingexperts.com/sap-fico-training-in-chennai/

suganya said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

sushma said...

SAP Success Factors Training in Chennai with Real Time Hands on Experience

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

sushma said...

Informative Blog...For a long time I was craving for a career growth in programming and then I came to know that THE CREATING EXPERTS is the one who provide training with hands on training and real time scenarios

http://thecreatingexperts.com/sap-abap-training-in-chennai/

contact 8122241286

ragu varan said...

Best SAP HANA Training in Chennai by leading HANA Consultant.
Reach at 9003085882
http://thecreatingexperts.com/category/sap-hana-training-in-chennai/

gilledashivani said...

Great information. the information provided is of great use as i got to learn new things. If you are looking for real time training in SAP ABAP, kindly contact sieve software
Sieve software offers real time training by industry experts with live projects and placement support.
SAP ABAP TRAINING IN HYDERABAD