Section Subtotal Patterns

Section subtotals in DFSORT are the count and total lines that appear after each control-break group (e.g. after each department). You get them by sorting by the section key and using OUTFIL SECTIONS= with TRAILER3=. TRAILER3 is written once per section and typically contains the section key value, COUNT= (records in that section), and TOTAL= (sum of a numeric field in that section). This page covers common patterns: single-level and multi-level section breaks, using SKIP= for spacing, including the section key in the trailer line, and ensuring data is sorted correctly so subtotals are right.

Report Generation
Progress0 of 0 lessons

Single-Level Section Subtotal

For one level (e.g. department): sort by the key that defines the section, then use SECTIONS= with that key and TRAILER3=.

text
1
2
3
4
5
6
7
8
SORT 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=(1:'GRAND TOTAL ',COUNT=(M11,LENGTH=10), ' ',TOTAL=(40,6,PD,LENGTH=14))

30,5 is the section key (department). After each department, TRAILER3 prints “Dept xxxxx” (the key), the section count, and the section total. SKIP=2L adds two blank lines. TRAILER1 prints once at the end with the grand count and total.

Sort Order and Section Key

The key in SECTIONS=(position,length,...) must match the sort order. So if you sort by (30,5,CH,A), use SECTIONS=(30,5,...). All records with the same value in 30,5 are in one section; when the value changes, TRAILER3 is written and a new section starts. If the data were not sorted by 30,5, the same department could appear in multiple chunks and each chunk would get its own TRAILER3, giving wrong subtotals.

Including the Section Key in TRAILER3

To print the current section key (e.g. department code) in the trailer line, reference its position and length in the TRAILER3= list. For example 30,5 inserts the 5-byte value at position 30 (the section key). So you get a line like “Dept SALES Count: 25 Total: 15000” where SALES comes from the key.

SKIP= for Spacing

SKIP=nL (or equivalent) inserts n blank lines after the section (after TRAILER3) before the next section. So sections are visually separated. Without SKIP, the next section’s first record follows immediately after the TRAILER3 line.

Multi-Level Section Breaks

Some products support multiple break levels: e.g. region (outer) and department (inner). You sort by region, then department. SECTIONS= defines both keys; you may have TRAILER3 for the inner break and TRAILER4 (or similar) for the outer break. When the inner key changes, the inner trailer is written; when the outer key changes, the outer trailer is written. Data must be sorted by all keys in order. Exact syntax (TRAILER3/TRAILER4, key order) is product-dependent.

Section subtotal checklist
ItemCheck
Sort keySort by the same (position, length) used in SECTIONS=.
TRAILER3 contentInclude section key (pos,len), COUNT=, TOTAL= with correct field position and format.
TRAILER1Use for grand total at end; separate from TRAILER3.
SKIPUse SKIP=nL if you want blank lines between sections.

Explain It Like I'm Five

Imagine a list of sales by store. You want a line after each store that says “Store X: 10 sales, total $500.” First you put all the same-store sales together (sort by store). Then after the last sale for store A you print “Store A: …” (that’s TRAILER3). Then you print two blank lines (SKIP), then the sales for store B. At the very end you print “All stores: 100 sales, $5000” (that’s TRAILER1). Section subtotal patterns are just that: sort by group, then after each group print a summary line.

Exercises

  1. Write SORT and OUTFIL with SECTIONS for section key at 1,10 (character), with TRAILER3 showing the key, section count, and total of a 6-byte PD field at 20. Add TRAILER1 for grand total.
  2. Why must the data be sorted by the section key?
  3. How would you get two blank lines between each section’s subtotal and the next section?

Quiz

Test Your Knowledge

1. What must be true about the data for SECTIONS= subtotals to be correct?

  • Data must be variable length
  • Data must be sorted by the section key so that all records in a group are consecutive
  • Data must have a header
  • TRAILER3 is optional

2. What does SKIP=2L in SECTIONS= do?

  • Skip 2 records
  • Leave 2 blank lines between the last detail record of a section and the TRAILER3 line (or before the next section)
  • Skip 2 sections
  • Only for TRAILER1

3. Where do you put the section key value (e.g. department name) in the TRAILER3 line?

  • Only in HEADER3
  • By referencing the key position and length in the TRAILER3= specification—e.g. 1,5 or 30,10 to insert the current section key value into the trailer line
  • TRAILER3 has no key
  • Only with COUNT=

4. Can you have more than one level of section break (e.g. region then department)?

  • No, only one level
  • Yes—SECTIONS= can define multiple break levels (e.g. position,length for each level), with different trailers (e.g. TRAILER3 for first level, TRAILER4 for second); data must be sorted by all break keys in order
  • Only with SUM
  • Only in HEADER

5. What is written first after the last detail record of a section: TRAILER3 or the next section's data?

  • Next section data
  • TRAILER3 is written immediately after the last detail record of the section; then the next section's records (and its HEADER3 if any) follow
  • TRAILER1 only
  • Nothing