# Cross-tabulate two columns

Shape / inspect

A variant of [Measure correlation](https://meridian.online/operations/measure-correlation).

Count the rows for each combination of values in two or more categorical columns, with association tests such as chi-square.

Page: <https://meridian.online/operations/measure-correlation/cross-tabulate-two-columns>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today.

## The DuckDB step

cross-tabulate two categorical columns, with a chi-square statistic

It reads the `orders` table the proofs share. Create it first:

```sql
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);
```

Then run the statement:

```sql
-- a full grid of product by region, so a combination that never occurs shows as 0 and not as a missing row
CREATE TEMP TABLE crosstab AS
WITH o AS (SELECT product, coalesce(region, 'unknown') AS region FROM orders),
     grid AS (SELECT p.product, r.region FROM (SELECT DISTINCT product FROM o) p CROSS JOIN (SELECT DISTINCT region FROM o) r)
SELECT g.product, g.region, count(o.product) AS n
FROM grid g LEFT JOIN o ON o.product = g.product AND o.region = g.region
GROUP BY g.product, g.region;
CREATE TEMP TABLE wide AS SELECT * FROM (PIVOT crosstab ON region USING sum(n) GROUP BY product) ORDER BY product;
-- Pearson chi-square: the sum over cells of (observed - expected)^2 / expected, with expected = row total * column total / N
CREATE TEMP TABLE chi AS
SELECT round(sum((n - expected) ^ 2 / expected), 4) AS chi_square,
       (count(DISTINCT product) - 1) * (count(DISTINCT region) - 1) AS degrees_of_freedom,
       round(sqrt(sum((n - expected) ^ 2 / expected) / (sum(n) * (least(count(DISTINCT product), count(DISTINCT region)) - 1))), 4) AS cramers_v
FROM (SELECT *, sum(n) OVER (PARTITION BY product) * sum(n) OVER (PARTITION BY region) / sum(n) OVER () AS expected FROM crosstab);
-- three ways: product by region by month, again on a full grid so empty combinations read 0
CREATE TEMP TABLE crosstab3 AS
WITH o AS (SELECT product, coalesce(region, 'unknown') AS region, strftime(order_date, '%Y-%m') AS month FROM orders),
     grid AS (SELECT p.product, r.region, m.month
              FROM (SELECT DISTINCT product FROM o) p CROSS JOIN (SELECT DISTINCT region FROM o) r CROSS JOIN (SELECT DISTINCT month FROM o) m)
SELECT g.product, g.region, g.month, count(o.product) AS n
FROM grid g LEFT JOIN o ON o.product = g.product AND o.region = g.region AND o.month = g.month
GROUP BY g.product, g.region, g.month;
SELECT * FROM wide;
SELECT * FROM chi;
SELECT * FROM crosstab3 WHERE n > 0 ORDER BY product, region, month;
SELECT CASE WHEN ((SELECT count(*) FROM crosstab) = 15 AND (SELECT sum(n) FROM crosstab) = 10 AND (SELECT count(*) FROM crosstab WHERE n = 0) = 7 AND (SELECT n FROM crosstab WHERE product = 'widget' AND region = 'west') = 2 AND (SELECT n FROM crosstab WHERE product = 'gizmo' AND region = 'east') = 2 AND (SELECT list(column_name) FROM (DESCRIBE wide)) = ['product', 'east', 'north', 'south', 'unknown', 'west'] AND (SELECT (chi_square, degrees_of_freedom, cramers_v) FROM chi) = (10.9722, 8, 0.7407) AND (SELECT count(*) FROM crosstab3) = 30 AND (SELECT sum(n) FROM crosstab3) = 10 AND (SELECT count(*) FROM crosstab3 WHERE n = 0) = 21 AND (SELECT n FROM crosstab3 WHERE product = 'gizmo' AND region = 'east' AND month = '2026-01') = 2 AND (SELECT n FROM crosstab3 WHERE product = 'widget' AND region = 'west' AND month = '2026-02') = 1 AND (SELECT count(*) FROM crosstab3 c JOIN (SELECT product, region, sum(n) AS n FROM crosstab3 GROUP BY product, region) m USING (product, region) JOIN crosstab x USING (product, region) WHERE m.n <> x.n) = 0) THEN 'as expected' ELSE error('the product by region counts were not 15 cells totalling 10 with 7 empty, or the chi-square was not 10.9722 on 8 degrees of freedom, or the product by region by month grid was not 30 cells with 21 empty') END AS verdict;
```

It needs nothing installed beyond DuckDB itself.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- Alteryx Designer: [`Contingency Table`](https://help.alteryx.com/current/en/designer/tools/data-investigation/contingency-table-tool.html) (read 2026-09-27)
- KNIME Analytics Platform: [`Crosstab`](https://api.hub.knime.com/nodes/*pNm03fXRSkMzHV8O?details=full) (read 2026-09-27)
