Control Break with SUM statements.
SUM can only be used within 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.
THE CODE IS GIVEN AS FOLLOWS TO SHOW THE EXAMPLE:
REPORT z_summation 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.
SORT it_ekpo.
LOOP AT it_ekpo INTO wa_ekpo.
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.
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.
SORT it_ekpo.
LOOP AT it_ekpo INTO wa_ekpo.
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.
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.
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.