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.
*-------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.
9 comments:
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
Nice Blog...
We offer SAP HR ONLINE TRAINING
Nice information.
And here you may get spraotechnologies.com More information.
Thanks for sharing Info on abap. Keep posting
Sum syntax is very useful for me. I am in Sap for the 1 year but today only i know this keyword.
SAP Success Factors Training in Chennai with Real Time Hands on Experience
http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
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
Great information. the information provided is of great
use as i got to learn new things. If you are looking for
real time training by industry experts with live projects
and placement support.kindly contact the creating experts
Reach our Technical Experts @ 8122241286 / 9003085882
Post a Comment