runtime
79 / 88

lesson 79 Warehouse & reporting projects · advanced

Product validity with LEAD & half-open intervals

Define product validity using the next change date.

the idea

A versioned table stores each version with the date it became valid. LEAD(valid_from) over the same product gives the next version’s start, which is this version’s exclusive end. An as-of query then picks the version with valid_from <= date < valid_to.

Dataset: Retail

product_versions4 columns · 75 rows
version_idnumber · unique idproduct_idtextcostdecimalvalid_fromdate
1207250.692010-01-01
2207260.692010-01-01
3207260.742011-03-01
4207260.732011-06-15
5207270.692010-01-01
6207270.782011-09-15
7207280.692010-01-01
8207280.742011-03-01
9209141.242010-01-01
10210340.42010-01-01
version_idnumber · unique id
1
product_idtext
20725
costdecimal
0.69
valid_fromdate
2010-01-01
version_idnumber · unique id
2
product_idtext
20726
costdecimal
0.69
valid_fromdate
2010-01-01
version_idnumber · unique id
3
product_idtext
20726
costdecimal
0.74
valid_fromdate
2011-03-01
version_idnumber · unique id
4
product_idtext
20726
costdecimal
0.73
valid_fromdate
2011-06-15
version_idnumber · unique id
5
product_idtext
20727
costdecimal
0.69
valid_fromdate
2010-01-01
version_idnumber · unique id
6
product_idtext
20727
costdecimal
0.78
valid_fromdate
2011-09-15
version_idnumber · unique id
7
product_idtext
20728
costdecimal
0.69
valid_fromdate
2010-01-01
version_idnumber · unique id
8
product_idtext
20728
costdecimal
0.74
valid_fromdate
2011-03-01
version_idnumber · unique id
9
product_idtext
20914
costdecimal
1.24
valid_fromdate
2010-01-01
version_idnumber · unique id
10
product_idtext
21034
costdecimal
0.4
valid_fromdate
2010-01-01

first 10 of 75 rows · your queries use all of them

worked example

The step marked “the pattern” is what the challenges build on. Running examples doesn’t count toward progress.

  1. List one product’s cost versions. Each row is a new cost starting on valid_from for product 20726—as-of queries pick the row whose interval contains your date.the pattern

    example.sql
    SELECT product_id, cost, valid_from
    FROM product_versions
    WHERE product_id = '20726'
    ORDER BY valid_from;
  2. Find products with many versions. Products at the top changed cost more than once in the period, so valid_to boundaries matter for historical pricing.

    step-2.sql
    SELECT product_id, COUNT(*) AS versions
    FROM product_versions
    GROUP BY product_id
    ORDER BY versions DESC
    LIMIT 10;
  3. Compute the next version with LEAD. LEAD(valid_from) over each product gives the next version’s start, which becomes this version’s exclusive end date.

    step-3.sql
    SELECT
      product_id,
      cost,
      valid_from,
      LEAD(valid_from) OVER (PARTITION BY product_id ORDER BY valid_from) AS next_valid_from
    FROM product_versions
    WHERE product_id = '20726';

common mistake

Using <= on both ends makes the boundary day match two versions.

challenge 1

Find the same product (NULL for the current version).

Return version_id, product_id, valid_from and valid_to, where valid_to is the next version’s valid_from.

challenge-1.sql
 
Output
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Product validity with LEAD & half-open intervals — Warehouse & reporting projects · Runtime