# Join on a range or nearest time

Join / range

inequality, interval, as-of, nearest-in-time join

Page: <https://meridian.online/operations/join-on-a-range-or-nearest-time>

## Status

Runs as a SQL step.

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

## The DuckDB step

the price in force on the order date

It reads the `orders` and `prices` tables the proofs share. Create them 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);

CREATE TABLE prices AS SELECT * FROM (VALUES
  ('widget', DATE '2026-01-01', 10.0),
  ('widget', DATE '2026-01-10', 11.0),
  ('gadget', DATE '2026-01-01', 20.0),
  ('gizmo',  DATE '2026-01-15', 33.0)
) t(product, valid_from, unit_price);
```

Then run the statement:

```sql
CREATE TEMP TABLE result AS
SELECT o.order_id, o.product, o.order_date, p.valid_from, p.unit_price
FROM orders o ASOF LEFT JOIN prices p ON o.product = p.product AND o.order_date >= p.valid_from ORDER BY o.order_id;
SELECT * FROM result;
SELECT CASE WHEN ((SELECT count(*) = 10 AND max(unit_price) FILTER (WHERE order_id = 1) = 10 AND max(unit_price) FILTER (WHERE order_id = 6) = 11 AND count(*) FILTER (WHERE order_id = 4 AND unit_price IS NULL) = 1 FROM result)) THEN 'as expected' ELSE error('an order did not take the price in force on its date') END AS verdict;
```

It needs nothing installed beyond DuckDB itself.

## Variants

- Join to the nearest earlier or later row: Match each row to the one row of another table whose ordered key is nearest before, after or either side.
- [Join on an inequality or interval](https://meridian.online/operations/join-on-a-range-or-nearest-time/join-on-an-inequality-or-interval): Match rows where a value falls in a range, intervals overlap, or another inequality or containment condition holds.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 86.2% of 312 rows (exact class, version 2).

- Apache DataFusion: [`ASOF JOIN`](https://datafusion.apache.org/user-guide/sql/select.html) (read 2026-09-27)
- EasyMorph: [`Interval merge`](https://help.easymorph.com/doku.php?id=transformations:intervalmerge) (read 2026-09-27)
- KNIME Analytics Platform: [`Subset Matcher`](https://api.hub.knime.com/nodes/*Rj5vxjLi7FjrFN15?details=full) (read 2026-09-27)
- Polars: [`DataFrame.join_asof`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- Polars: [`DataFrame.join_where`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- RapidMiner Studio: [`Calculate Overlaps`](https://docs.rapidminer.com/latest/studio/operators/extensions/Operator%20Toolbox/blending/attribute_generation/get_overlaps.html) (read 2026-09-27)
- Talend: [`tIntervalMatch`](https://help.qlik.com/talend/en-US/components/8.0/standardization/standardization-component) (read 2026-09-27)
