BP
BPS-Corp
DBA Tools & Training

2.3 History that changes

Lesson 2.3: History that changes

Numbers come from the sample file. In your own game this shows up only if a region changed owner.

The question: who owned this region then? If data only knows today's owner, it rewrites the past. The same happens with salespeople, prices and departments.

SET VARIABLE game = (SELECT game_id FROM dim_game ORDER BY started_utc DESC LIMIT 1);

By hand

1. The history table

SELECT region, owner, valid_from_month AS from_month, valid_to_month AS to_month, is_current
FROM rpt_region_owner_history
WHERE game_id = getvariable('game')
ORDER BY region, from_month;

Ridge has two rows: Rival for months 0 to 17, You from 18. 999999 means "still true". Keeping old versions like this is often called a Type 2 dimension.

2. Who owned Ridge in month 10?

SELECT r.name AS region, p.name AS owner
FROM dim_region r
JOIN dim_player p ON p.game_id = r.game_id AND p.player_id = r.owner_id
WHERE r.game_id = getvariable('game') AND r.name = 'Ridge'
  AND 10 BETWEEN r.valid_from_month AND r.valid_to_month;

Rival.

3. Wrong: today's owner

SELECT p.name AS owner, SUM(f.income) AS income
FROM fact_city_month f
JOIN dim_region r ON r.game_id = f.game_id AND r.region_id = f.region_id AND r.is_current
JOIN dim_player p ON p.game_id = r.game_id AND p.player_id = r.owner_id
WHERE f.game_id = getvariable('game')
GROUP BY p.name ORDER BY p.name;

You 483,320, and no Rival at all.

4. Right: the owner at the time

SELECT p.name AS owner, SUM(f.income) AS income
FROM fact_city_month f
JOIN dim_region r ON r.game_id = f.game_id AND r.region_id = f.region_id
  AND f.month BETWEEN r.valid_from_month AND r.valid_to_month
JOIN dim_player p ON p.game_id = r.game_id AND p.player_id = r.owner_id
WHERE f.game_id = getvariable('game')
GROUP BY p.name ORDER BY p.name;

Rival 34,392, You 448,928.

5. No version condition

Drop both conditions and every Ridge month matches both versions: 144 rows instead of 108.

6. A check, and its limit

Both owners together equal the plain total, 483,320. The wrong query in step 3 passes this check too. A matching total does not prove the right person got the money, so also check one month by hand.

With AI (optional)

In the video this starts at 2:58. No AI tool? Skip to Practice (4:08). Use a copy. What the AI reads is sent to its provider.

Asked: "Who owned Ridge in month 10?" and "How much income did each player earn over the whole game?" In a real run on 2026-12-05 (Claude) it said Rival, and 448,928 and 34,392. Check that the Rival has some income: if not, it used today's owner.

With Data Studio (optional)

In the video this starts at 3:35. No Data Studio? Skip to Practice (4:08). The same queries with the month condition, on the SQL Server copy, give the same answers.

Practice

  1. For how many months did the Rival own Ridge?
  2. How much income did the Rival earn from Ridge?
  3. How many regions did you own in month 17, and in month 18?

Answers (sample file)

1. 18. 2. 34,392. 3. 2, then 3. Queries: lesson-2.3-queries.sql, P1 to P3.

⬇ Downloads
← Previous