In this article we will see how can we find and calculate sum of amount field in SAP. The sample source code below will demonstrate how to achieve that:
REPORT ZBILL_REPORT. *TABLES: ZBILL. * DATA : WA_BILL TYPE ZBILL. " WORK AREA OR STRUCTURE DATA : IT_BILL TYPE ZBILL OCCURS 0. " INTERNAL TABLE DATA: V_SUM TYPE ZBILL-AMOUNT, V_REPID TYPE SY-REPID, V_USER TYPE SY-UNAME, V_DATE TYPE SY-DATUM, V_TIME TYPE SY-UZEIT. INITIALIZATION. V_REPID = SY-REPID. V_USER = SY-UNAME. V_DATE = SY-DATUM. V_TIME = SY-UZEIT. START-OF-SELECTION. " for selection of data from database. PERFORM GET_BILLS. END-OF-SELECTION. " this event signals that event of what has been initiated by start of selection event. PERFORM SHOW_BILLS. TOP-OF-PAGE. " Display header messages on all pages i.e. page header. WRITE: / 'Report :', 12 V_REPID, / 'Date :', 12 V_DATE dd/mm/yyyy, / 'User :', 12 V_USER, / 'Time :', 12 V_TIME. ULINE. END-OF-PAGE. WRITE: /40 'Page No.-', sy-pagno. FORM GET_BILLS. SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_BILL FROM ZBILL ORDER BY DOC_ID. " SELECT DATA FROM DATABASE TABLE AND PUT INTO INTERNAL TABLE. ENDFORM. FORM SHOW_BILLS. V_SUM = 0. LOOP AT IT_BILL INTO WA_BILL. " LOOP START AT FIRST. "---CONTROL BREAK STATEMENT, DISPLAY ONLY ONE TIME, BREAK THE LOOP FOR COLUMN HEADING. * FORMAT COLOR COL_BACKGROUND INTENSIFIED OFF. FORMAT COLOR COL_BACKGROUND INTENSIFIED ON. FORMAT COLOR 1 INTENSIFIED ON. WRITE: /3 'Document Id', 20 'Bill Number', 35 'Description', 70 'Date', 90 'Quantity', 110 'Amount (RS.)'. ULINE. " FOR UNDERLINE ENDAT. FORMAT COLOR 2 INTENSIFIED ON. * FORMAT COLOR 7 INTENSIFIED OFF. * FORMAT COLOR 7 INVERSE ON. WRITE: /3 WA_BILL-DOC_ID, 20 WA_BILL-BILNO, 35 WA_BILL-BILDESC, 70 WA_BILL-BILDATE, 80 WA_BILL-QUANTITY, 100 WA_BILL-AMOUNT. V_SUM = V_SUM + WA_BILL-AMOUNT. AT LAST. "---CONTROL BREAK STATEMENT, DISPLAY ONLY ONE TIME FORMAT COLOR 3 INTENSIFIED OFF. WRITE : /90 'Total:', 100 V_SUM. SKIP. "FOR BLANK LINE. FORMAT COLOR 2 INTENSIFIED OFF. WRITE: /40 '~~~~~~~ End Of Bills Display ~~~~~~~'. ENDAT. ENDLOOP. ENDFORM.
Output: