Show TOC

Calculating and Printing SubtotalsLocate this document in the navigation structure

Use

You want to print subtotals in accordance with a value in a different column. This column is then called the sort criterion.

Recommendation

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.

Note

Form SF_SUBTOTALS 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

  2. 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.

  3. 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).

  4. Within the loop calculate the subtotal. To do this, you need another program lines node ( E).

  5. 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-Schleife: interne Tabelle T_SFLIGHT INTO GS_SFLIGHT

  • Sortierkriterien: Feldname: CARRID Anfang/Ende Sortierbegriff: checked

B

CARRID Beginn Sortierstufe

C

  • Output parameter: GT_SUBTOTAL, GS_SUBTOTAL.

  • Program lines REFRESH GT_SUBTOTAL. CLEAR GS_SUBTOTAL.

D

&GS_SFLIGHT-PRICE&

E

  • Eingabeparameter: GS_SFLIGHT

  • Ausgabeparameter: GT_SUBTOTAL

  • Programmzeilen: 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 Ende Sortierstufe

H

LOOP-Schleife: interne Tabelle 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.