A back-test must only use what was known on each day. Apple reports in late July, so on 20 July the model sees the April report, not the July one. Prices arrive daily, reports arrive when they arrive, and the two never share a date: you cannot match them with =.
The idea in plain words
Where we are. The earlier joins matched equal keys. Here the key is time, and the match is "the latest one not after" or "anything inside this window". The next topic uses the date to build buckets of time, such as weekly and monthly bars.
Pairs each row with the latest row of another table whose time is not later than its own. It looks backwards only, so a result can never use information from the future.
Two tables with their own calendars
# The extra tables: one macro row a month, one filing row per quarterly reportprint(con.sql("SELECT date, cpi, unemployment FROM macro WHERE date >= '2026-06-01' ORDER BY date"))print(con.sql("SELECT symbol, filed_at, period_end, net_income FROM filings WHERE symbol = 'AAPL' ORDER BY filed_at DESC LIMIT 3"))
┌────────────┬─────────┬──────────────┐ │ date │ cpi │ unemployment │ │ date │ double │ double │ ├────────────┼─────────┼──────────────┤ │ 2026-06-01 │ 332.568 │ 4.2 │ │ 2026-07-01 │ 332.813 │ 4.1 │ │ 2026-08-01 │ 334.131 │ 4.1 │ │ 2026-09-01 │ 336.298 │ 4.2 │ └────────────┴─────────┴──────────────┘ ┌─────────┬────────────┬────────────┬─────────────┐ │ symbol │ filed_at │ period_end │ net_income │ │ varchar │ date │ date │ int64 │ ├─────────┼────────────┼────────────┼─────────────┤ │ AAPL │ 2026-07-31 │ 2026-06-27 │ 29789000000 │ │ AAPL │ 2026-05-01 │ 2026-03-28 │ 29578000000 │ │ AAPL │ 2026-01-30 │ 2025-12-27 │ 42097000000 │ └─────────┴────────────┴────────────┴─────────────┘
Joining the bars of KO to macro on b.date = m.date finds a match only on the first of a month, a day with a bar roughly one time in twenty. An as-of join gives every day the latest row dated on or before it.
# Equality matches the 1st of the month only; ASOF matches every dayprint(run(con, "SELECT count(*) FROM bars b JOIN macro m ON b.date = m.date WHERE b.symbol = 'KO' AND b.date >= '2026-05-01'"))print(run(con, "SELECT count(*) FROM bars b ASOF JOIN macro m ON b.date >= m.date WHERE b.symbol = 'KO' AND b.date >= '2026-05-01'"))print(con.sql(""" SELECT b.date, b.close, m.date AS macro_date, m.cpi1 FROM bars b ASOF JOIN macro m ON b.date >= m.date WHERE b.symbol = 'KO' AND b.date >= '2026-08-19' ORDER BY b.date"""))
[(3,)] [(78,)] ┌────────────┬────────┬────────────┬─────────┐ │ date │ close │ macro_date │ cpi │ │ date │ double │ date │ double │ ├────────────┼────────┼────────────┼─────────┤ │ 2026-08-19 │ 90.35 │ 2026-08-01 │ 334.131 │ │ 2026-08-20 │ 90.5 │ 2026-08-01 │ 334.131 │ │ 2026-08-21 │ 91.1 │ 2026-08-01 │ 334.131 │ └────────────┴────────┴────────────┴─────────┘
- 1
b.date >= m.dateis the as-of condition: the macro row is the latest one at or before the bar.
Put the equality part of the key first and the time comparison last: ASOF LEFT JOIN filings f ON f.symbol = b.symbol AND b.date >= f.filed_at matches within each symbol, and LEFT keeps the bars from before the first report, with NULL income, instead of dropping them.
# AAPL's first report was filed on 2009-07-22: before that day the income is NULLprint(con.sql(""" SELECT b.date, b.close, f.filed_at, f.net_income FROM bars b ASOF LEFT JOIN filings f ON f.symbol = b.symbol AND b.date >= f.filed_at WHERE b.symbol = 'AAPL' AND b.date BETWEEN '2009-07-20' AND '2009-07-23' ORDER BY b.date"""))
┌────────────┬─────────┬────────────┬────────────┐ │ date │ close │ filed_at │ net_income │ │ date │ double │ date │ int64 │ ├────────────┼─────────┼────────────┼────────────┤ │ 2009-07-20 │ 4.58231 │ NULL │ NULL │ │ 2009-07-21 │ 4.53886 │ NULL │ NULL │ │ 2009-07-22 │ 4.69632 │ 2009-07-22 │ 164000000 │ │ 2009-07-23 │ 4.72901 │ 2009-07-22 │ 164000000 │ └────────────┴─────────┴────────────┴────────────┘
Range joins and overlapping windows
A range join matches a row to every row inside a window, for instance the bars from a filing date to three days after it. Group by the filing to total the volume. The danger is overlap: a bar that sits in two windows is paired twice, so either de-duplicate with DISTINCT or ask with EXISTS.
# XOM bars within 120 days after a filing: the windows overlap, so rows repeatprint(run(con, """ SELECT count(*) AS joined_rows, count(DISTINCT b.date) AS distinct_days1 FROM bars b JOIN filings f ON f.symbol = b.symbol AND b.date BETWEEN f.filed_at AND f.filed_at + INTERVAL 120 DAY WHERE b.symbol = 'XOM'"""))print(run(con, """ SELECT f.filed_at, count(*) AS bars, sum(b.volume) AS total FROM filings f JOIN bars b ON b.symbol = f.symbol AND b.date BETWEEN f.filed_at AND f.filed_at + INTERVAL 3 DAY WHERE f.symbol = 'JPM' GROUP BY f.filed_at ORDER BY f.filed_at DESC LIMIT 2"""))
[(4268, 3581)] [(datetime.date(2026, 8, 6), 2, 10386317), (datetime.date(2026, 5, 1), 2, 13473538)]
- 1More joined rows than distinct days: some bars were counted twice.
The filings table is joined on filed_at, the day the report became public, not on period_end, the day the quarter ended. Using the quarter end would hand the model a result weeks before anyone could have seen it. Pick the date a fact became available, not the date it describes.
What the tasks ask
- 1`bars_with_macro` and `known_income`As-of joins; the second needs an equality on symbol and
LEFTto keep early bars. - 2`filing_window_volume`A range join with
BETWEEN, thenGROUP BYthe filing date. - 3`earnings_windows`A window on both sides of the filing, with each bar returned once.
Lab · what's graded
4 tasks · 100 ptsASOF JOIN:bars_with_macro25 pts- No look-ahead:
known_income25 pts - A range join:
filing_window_volume25 pts - Overlapping windows:
earnings_windows25 pts