Subtotals in DFSORT reports are the per-group sums and counts that appear after each control-break section—for example, "Department SALES: 25 records, Total 15000" followed by the next department's subtotal. You get them by using SECTIONS= with TRAILER3= and, inside the trailer, COUNT= (number of records in the section) and TOTAL= (or TOT=) to sum a numeric field over that section only. The grand total (over all records) goes in TRAILER1. Data must be sorted by the section key so that each group's records are consecutive. This page explains how to add section subtotals, how to format subtotal lines (including labeling them with the section key), and how to combine subtotals with a final grand total. For control-break structure (HEADER3, TRAILER3, SKIP=P), see Control break reporting; for report-level totals and IFTRAIL, see Report totals.
A subtotal is a count or sum for a subset of the report—one section or group (e.g. one department, one region). It is printed after the last record of that group. A grand total is the count or sum over all records and is printed once at the end of the report. In DFSORT you use TRAILER3 for subtotals (one per section) and TRAILER1 for the grand total. Both can use the same kinds of built-in functions: COUNT= for record count and TOTAL= (or TOT=) for the sum of a numeric field. The difference is scope: TRAILER3 is evaluated per section; TRAILER1 is evaluated over the whole file.
COUNT= inserts the number of records in the current scope. In TRAILER3, that scope is the current section—so you get the number of records in that department (or whatever the section key is). In TRAILER1, the scope is the entire report—so you get the total record count. Syntax is often COUNT=(format,LENGTH=n) where the format (e.g. M11, M10) controls editing (commas, zero suppression) and LENGTH is the output width. Example:
12TRAILER3=(1:'Records in section: ',COUNT=(M11,LENGTH=7)) TRAILER1=(1:'TOTAL RECORDS: ',COUNT=(M11,LENGTH=10))
The first line is repeated after each section (section count); the second appears once at the end (grand count). Exact format names (M11, etc.) are product-dependent; see your manual.
TOTAL= (or TOT=) sums a numeric field over the current scope. You specify the field's position and length in the input record, its format (PD for packed decimal, ZD for zoned decimal, BI for binary), and the output length. In TRAILER3 that sum is over the section only; in TRAILER1 it is over all records. Example: sum a 5-byte zoned decimal amount at position 40, output 12 characters:
12TRAILER3=(1:'Section total: ',TOTAL=(40,5,ZD,LENGTH=12)) TRAILER1=(1:'GRAND TOTAL: ',TOTAL=(40,5,ZD,LENGTH=12))
If the amount is packed decimal at position 40, use PD instead of ZD. The first two numbers in TOTAL=(start,len,...) are always the starting position and length of the field to sum; the format (ZD, PD, BI) must match the data. Wrong format can cause incorrect totals or abends.
Input: fixed-length records, department at 30,5 (character), sales amount at 40,6 (packed decimal). Sort by department, then use SECTIONS= with TRAILER3 for section count and section total, and TRAILER1 for grand count and grand total.
12345678SORT FIELDS=(30,5,CH,A) OUTFIL FNAMES=REPORT, SECTIONS=(30,5,SKIP=2L, TRAILER3=(1:'Dept ',30,5,' Count: ',COUNT=(M11,LENGTH=6), ' Total: ',TOTAL=(40,6,PD,LENGTH=12))), OUTREC=(1,80), TRAILER1=(2/,1:'GRAND TOTAL Count: ',COUNT=(M11,LENGTH=10), ' Total: ',TOTAL=(40,6,PD,LENGTH=14))
After each department you get a line like "Dept SALES Count: 25 Total: 15000". The 30,5 in TRAILER3 prints the current department code. At the end of the report, TRAILER1 prints the overall count and sum. SKIP=2L leaves two blank lines between sections. Format and keyword names may vary by product.
You can make the subtotal line easy to read by:
Example with alignment and blank line:
123TRAILER3=(2/,1:'--- Subtotal for ',30,5,' ---', 50:'Count: ',COUNT=(M11,LENGTH=6), 65:'Sum: ',TOTAL=(40,6,PD,LENGTH=12))
When you have two or more break levels (e.g. region and department), you can have a subtotal at each level. Sort by both keys (e.g. SORT FIELDS=(1,6,CH,A,30,5,CH,A)). In SECTIONS= you define both levels; the first level typically uses HEADER3/TRAILER3 and the second HEADER4/TRAILER4 (or similar). In each trailer you use COUNT= and TOTAL= for that level. So you might get a subtotal after each department (TRAILER4) and a higher-level subtotal after each region (TRAILER3). Exact parameter order and level numbers are product-dependent; see your DFSORT manual for multi-level SECTIONS=.
Besides COUNT= and TOTAL=, many DFSORT products allow MIN=, MAX=, and AVG= in trailer specifications. They compute the minimum, maximum, or average of a numeric field over the current scope (section for TRAILER3, whole report for TRAILER1). Useful for lines like "Min: 100 Max: 500 Avg: 250" per section or for the report. Syntax is similar to TOTAL=: position, length, format, and output length. Check your manual for exact keywords and format codes.
| Trailer | Scope |
|---|---|
| TRAILER3 (in SECTIONS=) | Per section: subtotal, section count, MIN/MAX/AVG for the section. |
| TRAILER1 | Whole report: grand total, total record count, MIN/MAX/AVG over all records. |
Some products support SUBCOUNT or similar keywords for cumulative or cross-section counts. The exact meaning (e.g. running total, count across a subset of sections) is product-dependent. When in doubt, use COUNT= and TOTAL= in TRAILER3 for section subtotals and in TRAILER1 for the grand total; that pattern is widely supported.
Imagine a list of toys by color. You want to know "how many red toys?" and "total price of red toys?" then the same for blue, then green. Each of those is a "subtotal"—a little total for one group. At the very end you want "how many toys in total?" and "total price of everything?"—that is the "grand total." In DFSORT, the little totals (subtotals) go in TRAILER3 after each color group, and the big total (grand total) goes in TRAILER1 at the end of the list.
1. Where do you put the subtotal for each group (e.g. each department) in a DFSORT report?
2. What does TOTAL=(20,6,PD,LENGTH=12) in a trailer mean?
3. How do you get both a subtotal per section and a grand total at the end?
4. Why must data be sorted by the section key when using subtotals in SECTIONS?
5. What is SUBCOUNT used for in DFSORT reports?