A sales report from keyed data
This guide builds a small sales report, the kind you would write in SQL or pandas. It totals sales by region, joins in customer details, filters, ranks and pivots by quarter.
The data
A table of six sales. Each has a region, a quarter, an amount and a customer.
enum Q = `q1 | `q2 | `q3 | `q4
sales := Table {region: [`east, `west, `east, `north, `west, `east], quarter: Q기 [0, 0, 1, 1, 2, 3], amount: [120, 80, 200, 50, 90, 160], cust: [`acme, `bolt, `acme, `core, `dune, `bolt]}
The quarters are an enum: a declared, ordered list of labels. Q기 [0, 0, 1, …] turns each number into the quarter at that position, so 0 is `q1. A plain label would do for most of this guide. The enum matters for the pivot at the end, because Garo then knows every quarter in advance, even one with no sales.
Total by region
댇 is reduce, keyed, sum. The keys come first, then the values:
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160]}
댇 sales.region sales.amountIn SQL: SELECT region, SUM(amount) FROM sales GROUP BY region. The regions come out in the order each first appears.
Swap the final for another aggregate: 댐 counts the sales per region, and 댕 averages them.
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160]}
댕 sales.region sales.amountSeveral numbers per region
To get the total and the count side by side, split the steps. 첮 groups the rows by region, sales[…] turns that into groups of rows, and F게 runs F on each group. F returns a record, one row of named values:
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160]}
{Record {total: 닫 x.amount, orders: 담 x.amount}}게 sales[첮 sales.region sales]In SQL: SELECT region, SUM(amount) AS total, COUNT(*) AS orders … GROUP BY region.
Each region's share
Divide each total by the grand total:
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160]}
totals := 댇 sales.region sales.amount
totals.value ÷ 닫 totals.valueThe east sells about 69% of the total.
Filter
F나 keeps the rows for which F is true. Inside F, x is the row:
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160]}
{x.amount ≥ 100}나 salesTo filter by a key, 냄 p keeps the rows whose key is one of p. It is SQL's WHERE region IN ('east', 'west'):
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160]}
냄 [`east, `north] sales.region salesTop sales
색 sorts the rows by a key, greatest first. 남 n keeps the first n rows. Together they are ORDER BY amount DESC LIMIT 2:
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160]}
남 2 (색 sales.amount sales)The same works on the totals. 샌 sorts by key, least first, so the weakest region comes to the top:
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160]}
totals := 댇 sales.region sales.amount
샌 totals.value totalsJoin in customer details
A second table gives each customer's tier. 잼 joins two tables on the column named by its parameter, keeping the rows that match. That is an inner join, SQL's JOIN customers USING (cust):
sales := Table {region: [`east, `west, `east], amount: [120, 80, 200], cust: [`acme, `bolt, `core]}
customers := Table {cust: [`acme, `bolt], tier: [`gold, `silver]}
잼 `cust sales customersThe sale to `core has no customer row, so the inner join dropped it. 잰 is the left join: it keeps every sale, and the missing tier is absent. The final is what differs: ㅁ keeps the exact matches, and ㄴ keeps all of the left side.
sales := Table {region: [`east, `west, `east], amount: [120, 80, 200], cust: [`acme, `bolt, `core]}
customers := Table {cust: [`acme, `bolt], tier: [`gold, `silver]}
잰 `cust sales customers잿 is the anti join: the sales with no customer, which is how you find the gaps.
sales := Table {region: [`east, `west, `east], amount: [120, 80, 200], cust: [`acme, `bolt, `core]}
customers := Table {cust: [`acme, `bolt], tier: [`gold, `silver]}
잿 `cust sales customersJoined, the sales can be totaled by tier:
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160], cust: [`acme, `bolt, `acme, `core, `dune, `bolt]}
customers := Table {cust: [`acme, `bolt, `core, `dune], tier: [`gold, `silver, `gold, `bronze]}
j := 잼 `cust sales customers
댇 j.tier j.amountRunning totals per region
랟 is scan, keyed, sum: a running total that restarts for each key. Each total stays in its row's place:
sales := Table {region: [`east, `west, `east, `north, `west, `east], amount: [120, 80, 200, 50, 90, 160]}
랟 sales.region sales.amountIn SQL: SUM(amount) OVER (PARTITION BY region ORDER BY …).
Pivot by quarter
A pivot turns the long table, one row per sale, into a wide one, with one column per quarter. F매 pivots, and F says how to combine two sales that land in the same cell. Its parameter names the row key, the column key and the values:
enum Q = `q1 | `q2 | `q3 | `q4
sales := Table {region: [`east, `west, `east, `north, `west, `east], quarter: Q기 [0, 0, 1, 1, 2, 3], amount: [120, 80, 200, 50, 90, 160]}
+매 [`region, `quarter, `amount] salesA region with no sales in a quarter gets an absent cell, not a 0. Fill them with 깊 0 when a 0 is what you mean. The column key must be an enum (or a Bool), so the columns are known before any row is read.
What you used
| Step | Garo | SQL |
|---|---|---|
| total per key | 댇 k v | SUM … GROUP BY |
| several aggregates per key | {Record {…}}게 t[첮 k t] | SELECT k, SUM, COUNT … GROUP BY |
| filter rows | F나 t | WHERE |
| filter by key | 냄 p k t | WHERE k IN (…) |
| sort, take n | 남 n (색 k t) | ORDER BY … DESC LIMIT n |
| inner, left, anti join | 잼 `c a b, 잰 `c a b, 잿 `c a b | JOIN, LEFT JOIN, NOT EXISTS |
| running total per key | 랟 k v | SUM … OVER (PARTITION BY …) |
| pivot | +매 [r, c, v] t | PIVOT |