runtime
CRM + ERP Integration · milestone 03 / 12

milestone 3/12 Product validity with LEAD & half-open intervals

Define product validity using the next change date.

CRM + ERP Integration · Follow extraction reconciliation, authoritative integration, date quarantine, quality gates, stable keys, replay-safe loads, and atomic promotion. Finish with a customer-grain capstone and field-level lineage.

approach

LEAD finds the next version start. Use [valid_from,valid_to) so a change date belongs to exactly one version; NULL end means current. This lab has unique starts per product. Latest-only output discards history; an as-of join needs all versions.

worked-example.sql
SELECT
  version_id,
  product_id,
  valid_from,
  LEAD(valid_from) OVER(PARTITION BY product_id ORDER BY valid_from,version_id) AS valid_to
FROM product_versions;

data for this milestone

product_versions5 columns · 4 rows
version_idinteger · keyproduct_idtextnametextcostrealvalid_fromtext
1P01Notebook82024-01-01
2P01Notebook92025-02-01
3P02Desk lamp302024-01-01
4P02Desk lamp322025-03-01

all 4 rows

challenge 1

Return version_id,product_id,valid_from,valid_to. valid_to is the next start for the same product, NULL for the latest version.

Return version_idproduct_idvalid_fromvalid_toany row order

challenge-1.sql
 
3 steps left · check both queries and pass the concept checkNext milestone
Product validity with LEAD & half-open intervals — CRM + ERP Integration · Runtime