﻿ Calculating and Printing Subtotals

# Calculating and Printing Subtotals

### Use

You want to print subtotals in accordance with a value in a different column. This column is then called the sort criterion. 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.

### Prerequisites

You are printing a table using the table or loop node.

### Procedure

We assume that you are already printing an internal table T_SFLIGHT of type TABLE OF SFLIGHT . Now we want to print the table sorted by airline and include a subtotal of the price after each airline.

1. Define two global fields for summing:
• GT_SUBTOTAL of type TABLE OF SFLIGHT
• GS_SUBTOTAL of type SFLIGHT
1. Go to the Data tab of the table node. In the Sort Criteria group box enter the fields by which you want to sort. In our example, this is the CARRID column. If you already sorted the table before you passed it to the form, mark Aleady Sorted so that SAP Smart Forms does not have to sort the table again.
2. Before printing the table lines of an airline carrier, you must clear the summing fields. To do this, mark the checkbox Event on Sort Begin for the criterion. In the navigation tree ( B) an event node appears whose inferior node you can use to clear the fields at the appropriate event (program lines node C).
3. Within the loop calculate the subtotal. To do this, you need another program lines node (E).
4. To print the subtotal after finishing the items of an airline, mark the checkbox Event on Sort End of the sort criterion. Another event node ( G) appears in the navigation tree, whose inferior node you can use to print the subtotal.

The facts explained above result in the following form logic:

Form logic for calculating a subtotal according to a criterion

 Navigation Tree Node Attributes/Contents/Conditions A Loop : internal table T_SFLIGHT INTO GS_SFLIGHT Sort criteria:Field name: CARRIDEvent on Sort Begin/End: marked B CARRID Beginning of sort level C Output parameters: GT_SUBTOTAL , GS_SUBTOTAL . Program lines: REFRESH GT_SUBTOTAL.CLEAR GS_SUBTOTAL. D &GS_SFLIGHT-PRICE& E Input parameter : GS_SFLIGHTOutput parameter : GT_SUBTOTALProgram lines :data: ls_sflight type sflight. ls_sflight-price = gs_sflight-price.ls_sflight-currency = gs_sflight-currency. collect ls_sflight into gt_subtotal. F &GS_SFLIGHT-CURRENCY& G CARRID End of sort level H Loop : internal table GT_SUBTOTAL INTO GS_SUBTOTAL I &GS_SUBTOTAL-PRICE& J &GS_SUBTOTAL-CURRENCY&

### Result

The subtotal (for each airline carrier) is printed before a break in the sort level.