Martin Gruber Understanding Sqlpdf Better
One of the most difficult concepts for SQL learners is the "Join." Gruber provides one of the most thorough treatments of this topic available.
Imagine you need to generate a PDF comparing this month’s sales to last month’s sales side-by-side. A standard GROUP BY won't work easily because you need two different time periods in the same row.
Gruber’s explanation of self-joins provides the solution: martin gruber understanding sqlpdf better
SELECT
current.product_id,
current.sales as sales_this_month,
previous.sales as sales_last_month,
(current.sales - previous.sales) as variance
FROM
(SELECT product_id, SUM(sales) as sales FROM monthly_sales WHERE month = 'June') as current
LEFT JOIN
(SELECT product_id, SUM(sales) as sales FROM monthly_sales WHERE month = 'May') as previous
ON current.product_id = previous.product_id;
This query generates a perfectly structured table that a PDF engine can render immediately as a side-by-side comparison. Without Gruber’s mental model of treating tables as reusable sets, you might have tried to do this comparison in the PDF scripting language—which is almost always slower and more error-prone.
PDFs are read top-to-bottom. SQL tables are unordered sets. Gruber is adamant that without an ORDER BY clause, the sequence of rows in your result set is arbitrary and subject to change. One of the most difficult concepts for SQL
The Gruber Principle: "If you care about the order, you must write ORDER BY. The database owes you no default order."
Application to SQLPDF: A shocking number of PDF reports have misaligned data or "random" row ordering because the developer assumed the primary key index would determine order. To master SQLPDF, you must always define a sort order that mimics the logical reading order of the report. This query generates a perfectly structured table that
While the book was written some time ago, it focuses heavily on the ANSI SQL standard.