SQLite WHERE and HAVING: Keep Group Totals Intact in a Drone Handoff Review
Compare filtering records before grouping with qualifying complete groups afterward, using a small invented drone handoff register and reproducible SQLite queries.

Define which population the report should count
A drone handoff register can group records by delivery package, inspection set or another agreed label. A reviewer may want to find groups with several records marked checked while still seeing how many records the complete group contains. Filtering to checked records before calculating the total answers a different question. It counts only the retained subset. This tutorial uses an invented register to make that distinction visible with SQLite WHERE and HAVING. It does not inspect a customer database or establish whether any NiVO delivery has been checked, approved or accepted. The flags and group labels are teaching data.
Write the intended question before writing the query. Here the useful question is which complete groups contain at least two checked records, along with each qualifying group’s full record count. A second report intentionally asks how many checked records survive an input filter. Both reports can be correct, but their totals describe different populations. Keep the wording next to the query so a recipient does not compare the two numbers as though a record vanished. The threshold of two is only a small demonstration choice. A real review criterion must come from the project’s own agreement.
Read the row and group stages separately
SQLite evaluates a WHERE condition against input rows before grouping. GROUP BY then collects rows with equal grouping expressions. HAVING evaluates a condition for each resulting group and discards groups that do not satisfy it. An aggregate HAVING expression examines the group’s rows. In the example, WHERE checked equals one removes unmarked records before COUNT is calculated, whereas HAVING SUM of checked at least two qualifies an already formed group. The latter can retain a full group total alongside the checked total. This is a difference in query meaning rather than a repair to the underlying register.
The local profile uses only nonempty ASCII group labels and integer flags zero or one. That makes SUM of the flags an explicit count of marked records. It avoids mixed storage types, unknown flags and a missing grouping label so the tutorial can isolate the processing stages. Those cases may matter in a real handoff and need their own documented treatment. Do not silently convert an empty or ambiguous flag into zero merely to fit this example. A report should show which inputs it accepts, which inputs it excludes and what each displayed total actually counts.

Product context only; this photograph does not depict the constructed examples in this article.
Run three queries on one fixed invented register
The following Python function creates an in-memory database, inserts the supplied teaching records and runs three read queries. It returns every group, a checked-only filtered summary and a complete-group summary qualified by its checked count. The connection closes afterward and no project database is opened. A bounded input size and restricted labels keep the demonstration small; they are not SQLite capacity limits. The final ordering only makes the printed examples easy to compare. It does not add a chronological or operational meaning to the group labels, and it does not alter the grouping population.
import sqlite3
def compare_groups(rows, minimum=2):
if not isinstance(rows, (list, tuple)) or len(rows) > 64:
raise ValueError("expected at most 64 teaching records")
if type(minimum) is not int or not 1 <= minimum <= 64:
raise ValueError("unsupported teaching threshold")
values = []
for row in rows:
if not isinstance(row, (list, tuple)) or len(row) != 2:
raise ValueError("expected group label and flag")
label, checked = row
if not isinstance(label, str) or not 1 <= len(label) <= 8:
raise ValueError("unsupported group label")
if not all("A" <= char <= "Z" for char in label):
raise ValueError("expected uppercase ASCII group label")
if type(checked) is not int or checked not in (0, 1):
raise ValueError("expected a zero or one flag")
values.append((label, checked))
con = sqlite3.connect(":memory:")
try:
con.execute("CREATE TABLE records(label TEXT, checked INTEGER)")
con.executemany("INSERT INTO records VALUES (?, ?)", values)
prefix = "SELECT label, COUNT(*), SUM(checked) FROM records "
all_groups = con.execute(prefix + "GROUP BY label ORDER BY label").fetchall()
before = con.execute(prefix + "WHERE checked = 1 GROUP BY label "
"HAVING COUNT(*) >= ? ORDER BY label", (minimum,)).fetchall()
after = con.execute(prefix + "GROUP BY label HAVING SUM(checked) >= ? "
"ORDER BY label", (minimum,)).fetchall()
return {"all": all_groups, "filtered_before": before, "qualified_after": after}
finally:
con.close()
records = [("A", 1), ("A", 1), ("A", 0),
("B", 1), ("B", 0), ("B", 0), ("C", 1)]
print(compare_groups(records))Read the two qualifying summaries correctly
The complete groups are A with three records and two checked, B with three and one checked, and C with one and one checked. At the teaching threshold of two, both qualifying queries return only A. Their counts differ: filtered_before reports A, two, two; qualified_after reports A, three, two. The latter retains A’s unmarked record in its full population while using the checked count to qualify the group. A report that labels both middle values simply total would obscure the distinction. Use labels such as checked-subset records and complete-group records when those are the intended meanings.
Change the threshold to one and compare again. The filtered summary counts two records for A and one each for B and C. The complete-group summary retains three for A, three for B and one for C. This is a useful independent check because the group membership agrees while the retained populations differ. Neither query edits a flag. Neither claims that a record marked checked passed a quality requirement. If a recipient needs to inspect the underlying records, provide a separate detail query tied to the same register revision rather than trying to infer every record from an aggregate row.
Avoid arbitrary detail columns in grouped reports
Keep the selected columns either grouping keys or the aggregate values needed for this report. Adding an unrelated filename to the SELECT list can suggest that one arbitrary detail represents an entire group. This example selects the label, COUNT and SUM only. If a reviewer needs filenames, build a separate detail view with an explicit relationship to the qualified labels. Preserve repeated records unless a distinctness rule has been established independently. An identical label and flag can still be a separate supplied record, and removing it during this exercise would change both the full count and the checked count.
Also keep the register revision fixed while comparing the reports. A changed input file, newly marked record or added package can explain a different result even when the query text is unchanged. Record the revision and threshold with the output, and retain the exact query used. If a real system permits NULL flags, decide explicitly how missing information should appear before adapting the SUM expression. The narrow zero-or-one profile here does not solve that problem. An unambiguous report is valuable because it exposes such decisions instead of hiding them behind a convenient aggregate.
Deliver totals with their population definitions
For an actual handoff, describe the grouping key, input filter, qualifying condition, counted population and data revision together. Include a small constructed example when it helps explain the distinction to another reviewer. Keep sensitive record content in the project’s established location. If the report definition changes, compare the versions against one unchanged input before interpreting the new numbers as progress or regression. The query can establish a reproducible summary of supplied flags. It cannot establish that those flags are accurate or replace the checks, evidence and responsible decisions required for delivery acceptance.
The NiVO catalogue photograph on this page supplies aircraft context only. It does not depict the invented register or demonstrate a product’s database behavior. The practical habit is to separate two questions: which rows enter the groups, and which completed groups qualify for the report. WHERE addresses the first stage; an aggregate HAVING condition addresses the second. Keeping that distinction in the report definition lets a drone review team retain full group totals without confusing them with a filtered subset. It also leaves record-level accuracy and the project’s acceptance meaning available for their separate review.



