QUERY GROUP BY and PIVOT: Summarise Records in 1 Easy Formula

This article contains product links. If you buy through them, we may earn a small commission at no extra cost to you.

A QUERY GROUP BY formula counts and totals records without a single helper column. Use GROUP BY in a Google Sheets QUERY formula to add up repeated print records, then use PIVOT to give each insert design its own column. For printing and assembling planner inserts, the result is a packing matrix that shows how many usable copies are ready and which design limits the number of complete sets. Google’s QUERY function reference has the full clause order.

If an A5 batch has 20 weekly inserts, 18 monthly inserts and 15 notes inserts, it can fill 15 sets that need one of each. A single total of 53 inserts won’t tell the packing team that, but QUERY GROUP BY can. The report needs to preserve the design breakdown and exclude copies still on hold.

Build the print and assembly report with QUERY GROUP BY

  1. Record one print run per row. Create a sheet called Print log with five headers in row 1: Batch, Size, Insert, Good copies and Status. Use one row for each run of a single size and design so QUERY GROUP BY can add them. If a weekly insert is printed twice for batch B01, enter two rows with the usable quantity from each run.

    Define Good copies as individual inserts ready to assemble once the run has been released. A duplex insert printed on one piece of paper counts as one insert, not two printed sides. If 14 copies come off the printer and two are spoiled during trimming, record 12. Don’t add a subtotal row (QUERY GROUP BY does the totals) or enter a cumulative count of 20 on an eight-copy top-up row.

    Use drop-downs for Size, Insert and Status if several people enter records. Notes and Notes page are different values to QUERY, so inconsistent names can split the same design across two columns. Keep Good copies numeric and put units in the header, not in entries such as 12 copies.

  2. Use GROUP BY to check the batch total by size. Put this formula on a separate report tab. Its last argument, 1, tells Sheets that the source range starts with one header row.

    =QUERY('Print log'!A1:E,
      "select B, sum(D)
       where A = 'B01' and E = 'Released' and D is not null
       group by B
       order by B
       label B 'Planner size', sum(D) 'Good copies'",
      1)
    

    WHERE chooses which source rows count before SUM(D) adds them. GROUP BY B returns one row per size, so two released A5 runs contribute to the same A5 total. For the clause order and the difference between filtering and sorting, see the guide to building a live report with SELECT, WHERE and ORDER BY.

    The size total is useful for reconciling finished production. If the next job is tracking paper stock across storage locations, the Tallyhive inventory tracker is a separate tool for purchases, production and stock movements. Keep the QUERY GROUP BY batch report as the count of inserts released for packing.

  3. Check the design totals before turning them into columns. In a second copy of the formula, change the first line of the query to select B, C, sum(D). Change the grouping to group by B, C, the sort to order by B, C and the label clause to label B 'Planner size', C 'Insert design', sum(D) 'Good copies'.

    The result has one row per size and design. With QUERY GROUP BY, two A5 Weekly runs of 12 and 8 should appear as one A5 Weekly row of 20. The rule is simple: each selected column must either be grouped or be inside an aggregate function such as SUM. Adding C to SELECT without adding it to GROUP BY makes the query invalid. This three-column QUERY GROUP BY report is often the easiest one to audit because each count sits next to the design name it belongs to.

  4. Use PIVOT to make the packing matrix. Keep size in GROUP BY, move design to PIVOT, and remove C from SELECT. Put the formula on a clear area of the report tab so its output has room to expand.

    =QUERY('Print log'!A1:E,
      "select B, sum(D)
       where A = 'B01' and E = 'Released' and D is not null
       group by B
       pivot C
       order by B
       label B 'Planner size', sum(D) ''",
      1)
    

    Instead of returning a row for A5 Weekly and another for A5 Notes, QUERY GROUP BY with PIVOT produces an A5 row with separate Weekly and Notes columns. Each cell holds the sum for that size and design. LABEL names the first column and removes the aggregate label so the design headers can stand on their own.

    Keep the batch filter if the report goes to a packing bench. Without it, released A5 copies from B01 and B02 would be added together even if the stock is in separate trays. If someone needs to switch batches without editing the formula, follow the guide to passing a date or a user-selected criterion into a query and use a batch drop-down.

  5. Turn the QUERY GROUP BY matrix into a release quantity. For a set that requires one copy of each design, use the lowest count across the required designs. If a set requires two Notes inserts, divide the Notes count by two and round down before comparing it with the other counts. Don’t include optional designs in that calculation.

    Treat a blank for a required design as zero available for packing, unless the source log still needs reconciling. A plain MIN over the pivot row isn’t enough: Sheets ignores blank cells, and the pivot can omit a design column entirely if no matching record contains it. For a repeatable packing template, keep a fixed list of required designs and use SUMIFS to pull released quantities by batch, size and design. Use the pivot for inspection and the fixed list for the release calculation.

Inventory Tracker Spreadsheet for Small Business | Tallyhive Stock, Sales & Reorder Planner for Google Sheets & Excel (.xlsx)

For stock control

Inventory Tracker Spreadsheet for Small Business | Tallyhive Stock, Sales & Reorder Planner for Google Sheets & Excel (.xlsx)
5.95

See it on taskdale.com →

Worked Example: Release and Assemble Batch B01

Enter these records in Print log!A1:E9. The A5 Weekly top-up is a separate run, the held Monthly copies aren’t cleared for packing, and B02 belongs to another job.

BatchSizeInsertGood copiesStatus
B01A5Weekly12Released
B01A5Weekly8Released
B01A5Notes15Released
B01A5Monthly18Released
B01PersonalWeekly9Released
B01PersonalNotes11Released
B01A5Monthly4Hold
B02A5Weekly6Released

Run the first formula. It returns 53 for A5 and 20 for Personal. A5 comes to 12 + 8 + 15 + 18; neither the four held Monthly copies nor the six Weekly copies from B02 belong in that total. In the design-level report, find the A5 Weekly row and check that it shows 20, not 12 or 26. That one check catches both a missed top-up and an incorrect batch filter.

Now run the pivot formula. It returns this matrix:

Planner sizeMonthlyNotesWeekly
A5181520
Personal119

If both sizes require one Monthly, one Notes and one Weekly insert, the release quantity is 15 A5 sets and zero Personal sets. The blank means no Personal Monthly row passed the filter. If Personal is sold as a two-design set with Notes and Weekly only, it can fill nine sets. The packing specification decides which reading is correct.

Use the matrix at the bench in this order:

  1. Set the packing order separately from the report order. The example matrix shows Monthly, Notes and Weekly. If the finished set should open with Monthly, followed by Weekly and then Notes, lay out the trays in that sequence. Don’t let the column order dictate the insert order.
  2. Assemble one proof set before releasing the stack. Print the insert PDF at actual size if the design is supplied at its finished dimensions. Measure the trimmed proof against the planner or an insert that already fits. For double-sided designs, turn the proof as it will be turned in the binder and check the reverse orientation before printing the full run. Check punch clearance against the inner text margin before punching the stack.
  3. Pack 15 A5 sets and reconcile the remainder. Pull 15 from each released A5 pile. The released remainder should be five Weekly, three Monthly and zero Notes. Leave the four held Monthly inserts in the hold tray; they weren’t included in the available stock and don’t resolve the Notes shortfall.
  4. Print a dated copy of the matrix if packing happens away from the screen. In Sheets, select the result with its headers and use File > Print > Selected cells. Include the batch identifier and the date on the copy, or write them on the sheet at the bench. Print the insert artwork separately from its PDF so report scaling can’t change the physical insert dimensions.

The production count also gives a useful reference for two separate records. For a shop selling the sets, LedgerBloom can track the paper expense and finished-set income in Google Sheets. Keep the count of 15 packed sets on the batch report and record money in the bookkeeping workbook.

If assembling inserts is part of a client’s retainer, Deskcrew fits the separate time record. Log the time spent printing, trimming and packing against the client there; the batch matrix remains the record of what was ready to hand over.

Bookkeeping Spreadsheet Google Sheets | LedgerBloom Small Business Income & Expense Tracker with Profit Dashboard, Budget, Tax Set-Aside & Mileage Log

For insert shop finances

Bookkeeping Spreadsheet Google Sheets | LedgerBloom Small Business Income & Expense Tracker with Profit Dashboard, Budget, Tax Set-Aside & Mileage Log
4.29

See it on taskdale.com →

Fix Missing Counts, Shifting Columns and Print Discrepancies

A Run Is in the Log but Missing From the QUERY GROUP BY Total

Check the batch and status against the actual filter values first. A row for B02 or a row marked Hold won’t appear in a report for released B01 stock. If both values match, check that the quantity is a number. Google Sheets QUERY infers a type for each source column and can treat minority types as null, so a text entry such as 8 copies in a mostly numeric column can disappear from the sum.

Check the header argument too. These formulas use A1:E with 1 because row 1 contains the headers. If the range starts at A2:E instead, use 0 so the first print run isn’t treated as a header. See the guide to why a spreadsheet query drops mixed-type values or misreads the header row for those two checks in more detail.

A Design Column Has Moved or Disappeared

The pivot builds headers from the design values present after filtering. If no released row in B01 contains Monthly, there is no Monthly column to read. In the example, the Monthly column exists because A5 has released copies; only the Personal cell is blank. Those two cases look different on the report, but neither supplies Personal Monthly stock.

Don’t build a packing calculation that assumes the second report column is always Monthly. For a fixed release sheet, put the target batch in H1, size in H2 and design in H3, then pull the quantity by its name:

=SUMIFS('Print log'!D2:D,'Print log'!A2:A,H1,
  'Print log'!B2:B,H2,'Print log'!C2:C,H3,
  'Print log'!E2:E,"Released")

For B01, Personal and Monthly, this returns zero. That makes it safe to include in a minimum across required designs after you’ve reconciled the log. If the pivot itself shows #REF! because it can’t expand, clear cells in its output area or move the formula to a dedicated tab. Don’t place manual packing notes immediately beside a matrix that can gain columns.

The Printer Count Is Higher Than the Packing Count

Check which unit each figure measures. Ten sheets printed two-up can yield 20 inserts before trimming, but one damaged insert leaves 19 usable pieces. A duplex run can also report 20 impressions for ten sheets. Enter the usable insert quantity in the log, not the printer’s sheet or impression count.

When a design is short, calculate the top-up against the target set quantity before printing it. For 20 A5 sets in B01, the released stock is short five Notes and two Monthly; Weekly already meets the target. Add only the usable copies from the top-up as new run records, then release the batch against a fresh matrix and a physical count of those two trays.

VA Agency Retainer & Hours Tracker Spreadsheet | Deskcrew Team Time Log, Hour Bank, Subcontractor Pay & Invoice Generator | Excel & Google Sheets

For client assembly work

VA Agency Retainer & Hours Tracker Spreadsheet | Deskcrew Team Time Log, Hour Bank, Subcontractor Pay & Invoice Generator | Excel & Google Sheets
16.99

See it on taskdale.com →

Scroll to Top