Calculating and Printing Grand Totals
Use
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:
The solution described here refers to
printing old tables. However, it can easily be adapted for the new table node type.
Form
Prerequisites
You are already printing the table using the table node, for which you now want to print a grand total.
Procedure
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.Form logic for calculating the grand total
Navigation Tree |
Node Attributes/Contents/Condititons | |||
|
Output parameters : GT_TOTAL, GS_4NEXT_ADDITIONProgram lines: refresh gt_total. clear: gs_total, gs_4next_addition. | |||
|
Loop :internal table T_SFLIGHT INTO GS_SFLIGHT | |||
|
|
&GS_SFLIGHT-PRICE& | |||
|
|
Input parameters : GS_SFLIGHT, GS_4NEXT_ADDITIONOutput parameters : GT_TOTAL, GS_4NEXT_ADDITION Program lines : if not gs_4next_addition is initial. collect gs_4next_addition into gt_total. endif. gs_4next_addition-price = gs_sflight-price. | |||
|
|
&GS_SFLIGHT-CURRENCY& | |||
|
Footer |
Make sure to assign enough height to the footer. | |||
|
|
Condition: only at end of section Input parameter : GS_4NEXT_ADDITIONOutput parameter : GT_TOTAL Program lines : if not gs_4next_addition is initial. collect gs_4next_addition into gt_total. endif. | |||
|
|
Loop :internal table GT_TOTAL INTO GS_TOTAL | |||
|
|
&GS_TOTAL-PRICE& | |||
|
|
&GS_TOTAL-CURRENCY& | |||
Result
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).Discussion
Some steps within the form logic are self-explanatory:
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:
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 :