{
  "schema": "vedokrok.public-item.v1",
  "release_id": "VEDOKROK-SEL-5d86e1b23401e578",
  "url": "/knowledge/aggregate-each-detail-table-before-joining-two-totals",
  "id": "MHC-D-RESEARCH-9719",
  "version": "0.1.0",
  "title": "Aggregate each detail table before joining two totals",
  "summary": "A join can inflate both totals at once.",
  "kind": "protocol",
  "body": "When the report needs one total per parent, sum each child set by the same complete parent key before joining. Use LEFT JOIN if parents without child rows must remain. Do not sum multiplied detail rows and expect GROUP BY to repair the totals.",
  "limits": [
    "Pre-aggregation can erase needed invoice-payment pairings. An absent or NULL total needs a stated meaning; do not silently replace it with zero."
  ],
  "topics": [
    "critical-thinking"
  ],
  "intents": [],
  "source_ids": [
    "pg-joins",
    "pg-agg"
  ],
  "evidence": [
    {
      "claim": "In PostgreSQL, a qualifying join produces an output row for each matching row pair; joining two independent one-to-many detail tables through the same parent can multiply rows before aggregation.",
      "source_id": "pg-joins",
      "role": "supports",
      "note": "Claim limit: This describes row multiplicity, not a universal query planner rule; choose aggregation only when the report needs one total per parent, and preserve row pairings when they are meaningful.\nSource reading history: selected_passages; 2026-10-11; Sections 7.2.1.1 Joined Tables; 7.2.1.3 Subqueries; 7.2.2 WHERE; 7.2.3 GROUP BY. Version 18 passages rechecked on 2026-10-11: matching row pairs, full ON conditions, derived tables, filtering and grouping. Grounds SQL semantics, not every engine's behavior or the effectiveness of the drafted reporting workflow.\nAgent release review: PostgreSQL 18 table expressions, NULL comparisons and aggregates, the UK Government Data Quality Framework and Microsoft Power Query profiling documentation were checked at the cited sections. The original examples and scope limits remain attached.",
      "locator": "Sections 7.2.1.1 Joined Tables and 7.2.1.3 Subqueries"
    },
    {
      "claim": "In PostgreSQL, LEFT JOIN adds a row with NULL right-hand values when no pair satisfies ON. A later WHERE condition keeps only rows for which it is true; a right-hand equality test rejects these NULL rows, while a NULL-accepting condition can retain them.",
      "source_id": "pg-joins",
      "role": "supports",
      "note": "Claim limit: PostgreSQL 18 logical row semantics, not a planner or performance guarantee. A NULL-accepting WHERE condition does not recreate rows already lost because their key matched only nonqualifying right-hand records.\nSource reading history: selected_passages; 2026-10-11; Sections 7.2.1.1 Joined Tables; 7.2.1.3 Subqueries; 7.2.2 WHERE; 7.2.3 GROUP BY. Version 18 passages rechecked on 2026-10-11: matching row pairs, full ON conditions, derived tables, filtering and grouping. Grounds SQL semantics, not every engine's behavior or the effectiveness of the drafted reporting workflow.\nAgent release review: PostgreSQL 18 table expressions, NULL comparisons and aggregates, the UK Government Data Quality Framework and Microsoft Power Query profiling documentation were checked at the cited sections. The original examples and scope limits remain attached.",
      "locator": "Section 7.2.1.1 LEFT OUTER JOIN and ON-versus-WHERE example; Section 7.2.2 WHERE"
    },
    {
      "claim": "COUNT(*) counts rows while COUNT(expression) counts only input rows with a non-null expression.",
      "source_id": "pg-agg",
      "role": "supports",
      "note": "Claim limit: Scalar expression counting in PostgreSQL 18. These are counts of input rows, not automatically distinct entities or valid field values; join and filter rules determine the input. No planner or performance guarantee.\nSource reading history: selected_passages; 2026-10-11; Section 9.21, Table 9.62: count and sum; paragraph on aggregates with no selected rows. PostgreSQL 18 count and sum definitions, including empty-input behavior, rechecked on 2026-10-11. Selected passages ground the row/value/total distinction, not production execution or the effectiveness of the drafted reporting workflow.\nAgent release review: PostgreSQL 18 table expressions, NULL comparisons and aggregates, the UK Government Data Quality Framework and Microsoft Power Query profiling documentation were checked at the cited sections. The original examples and scope limits remain attached.",
      "locator": "Section 9.21 Aggregate Functions, count"
    },
    {
      "claim": "In PostgreSQL 18, SUM adds non-null input values; if no rows are selected, it returns NULL rather than zero.",
      "source_id": "pg-agg",
      "role": "supports",
      "note": "Claim limit: Group creation and join type are separate questions. A NULL sum alone does not establish the cause of missing data or a numerical zero; scalar totals also do not establish that all input amounts were present.\nSource reading history: selected_passages; 2026-10-11; Section 9.21, Table 9.62: count and sum; paragraph on aggregates with no selected rows. PostgreSQL 18 count and sum definitions, including empty-input behavior, rechecked on 2026-10-11. Selected passages ground the row/value/total distinction, not production execution or the effectiveness of the drafted reporting workflow.\nAgent release review: PostgreSQL 18 table expressions, NULL comparisons and aggregates, the UK Government Data Quality Framework and Microsoft Power Query profiling documentation were checked at the cited sections. The original examples and scope limits remain attached.",
      "locator": "Section 9.21, Table 9.62 sum and paragraph on no selected rows"
    }
  ],
  "use_when": [
    "Two one-to-many detail tables are both joined to the same parent."
  ],
  "avoid_when": [
    "Pre-aggregation can erase needed invoice-payment pairings. An absent or NULL total needs a stated meaning; do not silently replace it with zero."
  ],
  "steps": [
    "Compute invoice and payment totals independently by customer, with the same intended filters.",
    "Keep child row and non-null amount counts alongside totals when missing data must be distinguished.",
    "Check known totals, missing child records and missing amounts before assigning any zero defaults."
  ],
  "example": "Four invoices totalling 100 and three payments totalling 18 can yield sums of 300 and 72 in a detail join. Separate aggregates retain 100 and 18.",
  "check": "Totals match the independent sums, required parents survive, and missing-data cases remain distinguishable from a recorded zero.",
  "published_at": "2026-11-12T18:37:40Z",
  "review": {
    "kind": "agent",
    "label": "Agent editorial and scoped source review",
    "reviewed_at": "2026-11-12T18:37:40Z"
  },
  "knowledge_origin": {
    "release_id": "MHC-RPUB-20261112-20287590",
    "corpus_commit": "20287590ddd3999b7492df232822b6a0f7d114b1",
    "manifest_sha256": "2a987660d12de0ce2bde77a421424f7762af1ff77872ef41093fecc33db74a95",
    "projection_sha256": "2807da39bbe6a3dee2db32a76675ac5185f5a33e8b0a5cdd0c22a4731f3378a0",
    "published_at": "2026-11-12T18:37:40Z"
  },
  "editorial_overlay": null,
  "editorial": null,
  "notices": [],
  "sources": [
    {
      "id": "pg-joins",
      "title": "PostgreSQL 18: Table Expressions",
      "url": "https://www.postgresql.org/docs/18/queries-table-expressions.html"
    },
    {
      "id": "pg-agg",
      "title": "PostgreSQL 18: Aggregate Functions",
      "url": "https://www.postgresql.org/docs/18/functions-aggregate.html"
    }
  ],
  "relations": [],
  "collections": [
    {
      "id": "RC-MHC-UNION-DATA-JOINS-QUALITY-DIAGNOSTICS-2026",
      "title": "Diagnose data joins and quality before trusting a report",
      "url": "/collections/diagnose-data-joins-and-quality-before-trusting-a-report"
    }
  ],
  "topic_hubs": [
    {
      "id": "critical-thinking",
      "title": "Critical thinking and evidence",
      "url": "/topics/critical-thinking"
    }
  ],
  "solution_packs": []
}
