When you need it
Analytics engineer screens ask about the shape of data, not just the answers. "Is order id unique in this table?", "what tier was the customer on the day of the order?", "show every day, even days with no sales", "convert each order to USD using the rate on that day". Words that signal it: "grain", "history", "as of", "valid from", "spine", "every day", "latest before".
The job is to make the table's grain explicit, then join other tables to it without changing the grain.
The shape of the query
Checking grain:
select count(*) as rows_total, count(distinct order_id) as distinct_orders
from stg_orders;If the two numbers differ, the table is not one row per order.
A type 2 history table has one row per version, valid from valid_from up to, but not including, valid_to. The version in force on a date is:
join dim d
on d.customer_id = f.customer_id
and f.ts >= d.valid_from
and (f.ts < d.valid_to or d.valid_to is null)Step by step:
- State the grain of each input in one sentence.
- Reduce the fact table to its grain (see the deduplication lesson) before joining.
- Join to history on the key and the date range, half-open (
>=start,<end). - Re-check that the row count of the result equals the fact table's.
with latest as (
select order_id, customer_id, order_total, updated_at,
row_number() over (partition by order_id order by updated_at desc, loaded_at desc) as rn
from stg_orders
)
select l.order_id, l.order_total, h.tier, h.region
from latest l
join dim_customers_history h
on h.customer_id = l.customer_id
and l.updated_at >= h.valid_from
and (l.updated_at < h.valid_to or h.valid_to is null)
where l.rn = 1
order by l.order_id
limit 10;A date spine lists every date so quiet days show as zero. An as-of join then picks the latest rate on or before each day:
with spine as (
select d::date as day
from generate_series(date '2025-01-01', date '2025-01-14', interval '1 day') as g(d)
)
select s.day,
(select r.usd_per_unit
from fx_rates r
where r.currency = 'EUR' and r.rate_date <= s.day
order by r.rate_date desc
limit 1) as eur_rate_as_of
from spine s
order by s.day;Weekends have no rows in fx_rates, so the lookup returns Friday's rate for Saturday and Sunday.
Traps interviewers look for
- Join fan-out from history. If two versions overlap, or
valid_toequals the nextvalid_fromand you use<=, a fact matches two versions and rows double. Use a half-open interval. - NULL
valid_to. The current version has no end.d < NULLis unknown, so includeor valid_to is null, or coalesce to a far future date. - Joining to the current row only. Joining orders to today's customer tier says what tier they are, not what they were when they ordered.
- Sparse dates. Grouping by day skips days with no rows. Left join from a spine, and use
coalesce(sum(x), 0). - Weekend and holiday gaps in rates. An equality join on date drops weekend orders. Use an as-of join.
- Currencies. An order in USD has no rate row. Do not let the join delete it. Use a left join and
coalesce(rate, 1)for the base currency. - Wrong grain after the join. Always compare the row count before and after.
Variations
- Slowly changing dimension build. Use
lead(effective_on)as the end date of each row. - Snapshots to changes. Compare each day's snapshot with the previous day's using
lagto find additions and removals. - Fill forward. Carry the last known value over missing days, with an as-of join or
last_value(... ignore nulls)where supported. - Uniqueness tests.
having count(*) > 1on the claimed key is the standard test. - Latest per key with
QUALIFY-style logic. Shown in the deduplication lesson.
In other warehouses
- Snowflake has
ASOF JOINwithMATCH_CONDITION:from orders o asof join fx_rates r match_condition (o.day >= r.rate_date) on o.currency = r.currency. It returns the closest earlier row without a subquery or window. - Where there is no
ASOF JOIN(PostgreSQL, BigQuery, SQL Server), join onrate_date <= day, then keep the latest withrow_number() ... = 1(withQUALIFYin BigQuery). - SQL Server writes the lookup as
CROSS APPLY (select top 1 ... order by rate_date desc)orOUTER APPLYto keep unmatched rows. - Date spines. BigQuery uses
UNNEST(GENERATE_DATE_ARRAY(start, end)). Snowflake uses aGENERATORtable withDATEADD. SQL Server and MySQL use a recursive CTE. See the dialect cheat sheet. - Fill forward.
LAST_VALUE(x IGNORE NULLS)works in Snowflake and BigQuery. PostgreSQL does not supportIGNORE NULLS.