Calculating and Printing Grand Totals


You want to calculate a grand total for a column of your table and print it. If you wish, you can print the grand total at the end of the table and subtotals at the end of each page.

If you do not want to calculate totals for different currencies or quantity units, use the Calculations tab of the table node (see: Calculations).

The solution described here refers to printing old tables. However, it can easily be adapted for the new table node type.

Form SF_TOTALS in the system illustrates how to print grand totals for different currencies using the old or the new table types. Use application program SF_TOTALS to call this form.


You are already printing the table using the table node, for which you now want to print a grand total.


To print a subtotal of the grand total before a page break, the subtotal must be calculated in a special way. However, you can also use this method if you only want to print the grand total at the end of the table.

We will use the flight data model to explain this summing method. A table containing the flights of the airline carriers is already being printed in a form. The corresponding internal table ( T_SFLIGHT ) is defined in the form interface and has type TABLE OF SFLIGHT . To print a table line, you have defined field GS_SFLIGHT of type SFLIGHT as a global field. You now want to calculate the total on the flight prices, taking into account that the airlines use different currencies to indicate their prices.

  1. Create three additional fields in the global definition:
    • GS_TOTAL of type SFLIGHT
  1. In the navigation tree, you are already printing the table in the main window using table node B. To keep it simple, the table output in our example (see below) consists only of the nodes C (containing the price) and E (containing the currency). Enhance the form logic as follows:

Form logic for calculating the grand total

Navigation Tree

Node Attributes/Contents/Condititons



Output parameters : GT_TOTAL, GS_4NEXT_ADDITION
Program lines:
refresh gt_total.
clear: gs_total, gs_4next_addition.



Loop :








Input parameters : GS_SFLIGHT, GS_4NEXT_ADDITION
Output parameters : GT_TOTAL, GS_4NEXT_ADDITION
Program lines :
if not gs_4next_addition is initial.
   collect gs_4next_addition into gt_total.

gs_4next_addition-price = gs_sflight-price.
gs_4next_addition-currency = gs_sflight-currency.







Make sure to assign enough height to the footer.




Condition: only at end of section

Input parameter : GS_4NEXT_ADDITION
Output parameter : GT_TOTAL
Program lines :
if not gs_4next_addition is initial.
   collect gs_4next_addition into gt_total.




Loop :
internal table GT_TOTAL INTO GS_TOTAL








If the table output covers several pages, a subtotal of the grand total appears at the end of each page. If you want to print a total only at the end of the table, include the condition only at end of section into the loop node ( G).


Some steps within the form logic are self-explanatory:

  • To be able to calculate the price in accordance with the currency, we need the internal table GT_TOTAL . We can then use the ABAP statement COLLECT to add up the values, thereby easily assigning each new value to the correct currency. Each entry in table GT_TOTAL corresponds to the totals value in one currency.
  • At the beginning of the table, all summing fields must be cleared (node A).
  • When printing the footer, we use loop G to print the totals sorted by currencies.

Other steps, in contrast, are hard to understand at first sight, for example, what do we need field gs_4next_addition for?. Why don't we simply add the current value of gs_sflight to the corresponding value in the internal table GT_TOTAL ?

The reason for this lies in the output control for loops and tables. Determining the page break requires the output control to buffer table lines before printing them. The output control needs the height specifications of the entire line before it can determine when to trigger the automatic page break. The program lines nodes, in contrast, are not buffered but executed directly during processing.

See also: Processing Loops and Tables

The output control recognizes that the last table line is finished as soon as it starts processing the next line. To synchronize the calcuation of the total with its output, the calculation must be delayed. And for this reason we need the additional field gs_4next_addition :

  1. Within the loop, the total is calculated after the first output (node D). The node can even be positioned further down in the tree. The important fact is that the output control has recognized that a new table line starts (with the new table type, this is achieved by explicitly creating table lines). If this last line does not fit onto the page, the output control jumps to the footer processing. In this case, node D is not processed. This would not have happened if node D had been inserted before node C.
  2. If no new table line starts, the value of the last line above node D is added to the total. Since gs_4next_addition is empty at the beginning, we must query this in the programming. Otherwise, the COLLECT statement would use an empty currency in GT_TOTAL . Then, we temporarily store the current value of gs_sflight in gs_4next_addition , before we add it to the total in the next loop pass. This means that we do the same thing that the output control does with the table line: We buffer the value of this line.
  3. After all table lines have been processed, one last calculation is missing, which we execute in the footer. To make sure that this last addition happens only at the end of the table output, we enter the appropriate condition in the program lines node. Here, again, we query whether gs_4next_addition is empty to catch the case that T_SFLIGHT might have no entries.