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.amount
⇒ Table {key: [`east, `west, `north], value: [480, 170, 50]}Try it

In 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.amount
⇒ Table {key: [`east, `west, `north], value: [160.0, 85.0, 50.0]}Try it

Several 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]
⇒ Table {key: [`east, `west, `north], total: [480, 170, 50], orders: [3, 2, 1]}Try it

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.value
⇒ [0.6857142857142857, 0.24285714285714285, 0.07142857142857142]Try it

The 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}나 sales
⇒ Table {region: [`east, `east, `east], amount: [120, 200, 160]}Try it

To 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 sales
⇒ Table {region: [`east, `east, `north, `east], amount: [120, 200, 50, 160]}Try it

Top 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)
⇒ Table {region: [`east, `east], amount: [200, 160]}Try it

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 totals
⇒ Table {key: [`north, `west, `east], value: [50, 170, 480]}Try it

Join 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 customers
⇒ Table {cust: [`acme, `bolt], region: [`east, `west], amount: [120, 80], tier: [`gold, `silver]}Try it

The 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
⇒ Table {cust: [`acme, `bolt, `core], region: [`east, `west, `east], amount: [120, 80, 200], tier: [`gold, `silver, `none]}Try it

잿 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 customers
⇒ Table {region: [`east], amount: [200], cust: [`core]}Try it

Joined, 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.amount
⇒ Table {key: [`gold, `silver, `bronze], value: [370, 240, 90]}Try it

Running 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.amount
⇒ [120, 80, 320, 50, 170, 480]Try it

In 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] sales
⇒ Table {region: [`east, `west, `north], q1: [120, 80, `none], q2: [200, `none, 50], q3: [`none, 90, `none], q4: [160, `none, `none]}Try it

A 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

StepGaroSQL
total per key댇 k vSUM … GROUP BY
several aggregates per key{Record {…}}게 t[첮 k t]SELECT k, SUM, COUNT … GROUP BY
filter rowsF나 tWHERE
filter by key냄 p k tWHERE 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 bJOIN, LEFT JOIN, NOT EXISTS
running total per key랟 k vSUM … OVER (PARTITION BY …)
pivot+매 [r, c, v] tPIVOT