BP
BPS-Corp
DBA Tools & Training

1.3 Names, not numbers

Lesson 1.3: Names, not numbers

Numbers come from the sample file; yours will differ.

The question: where did my money go? The money is in one table and the names are in others. Putting them together is a join. In Excel you did this with a lookup.

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

By hand

1. Money lines

SELECT month, region_id, category, amount
FROM fact_budget_line
WHERE game_id = getvariable('game') AND month = 12 AND region_id = 1;

Plus came in, minus went out. One row is one money line: one region, one month, one category.

2. Join to get the name

SELECT r.name AS region, b.category, b.amount
FROM fact_budget_line b
JOIN dim_region r ON r.game_id = b.game_id AND r.region_id = b.region_id AND r.is_current
WHERE b.game_id = getvariable('game') AND b.month = 12 AND r.name = 'Midland';

ON says which rows belong together. is_current picks the region's latest version (more in lesson 2.3).

3. Where did my money go?

SELECT b.category, SUM(b.amount) AS amount
FROM fact_budget_line b
JOIN fact_city_month f ON f.game_id = b.game_id AND f.region_id = b.region_id AND f.month = b.month
JOIN dim_player p ON p.game_id = f.game_id AND p.player_id = f.owner_id
WHERE b.game_id = getvariable('game') AND p.name = 'You'
GROUP BY b.category
ORDER BY amount;

Power plant 27,000, buildings 11,874, roads 4,219 spent; taxes brought in the rest. Only months you owned the region count.

4. Spending by region

SELECT r.name AS region, -SUM(b.amount) AS spent
FROM fact_budget_line b
JOIN dim_region r ON r.game_id = b.game_id AND r.region_id = b.region_id AND r.is_current
WHERE b.game_id = getvariable('game') AND b.amount < 0
GROUP BY r.name
ORDER BY r.name;

Harbor 13,079, Midland 21,095, Ridge 16,135 (all regions, whoever owned them).

5. The join that doubles

Leave out AND r.is_current and there is no error, but Ridge becomes 32,270. Ridge has two rows in dim_region, one for each owner, so every money line matched twice.

6. Check it

SELECT -SUM(amount) AS spent
FROM fact_budget_line
WHERE game_id = getvariable('game') AND amount < 0;

50,309, with no join at all. Joined totals must add up to the same. A join must not change a total.

With AI (optional)

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

Asked: "Where did my money go?" and "How much did each region spend?" In a real run on 2026-12-05 (Claude) it found 43,093 spent while the regions were yours, mostly the power plant; showed all spending and spending while yours; and did not double Ridge. It also said its starting-cash figure (15,000 a region) was an inference. Check that region totals add up to 50,309.

With Data Studio (optional)

In the video this starts at 3:35. No Data Studio? Skip to Practice (4:04). The same two joins on the SQL Server copy return the same category and region totals.

Practice

  1. How much did you spend on the power plant while the regions were yours?
  2. Which region paid the most for roads?
  3. How many money lines does one region have in one month?

Answers (sample file)

1. 27,000. 2. Midland, 2,385. 3. 6. Queries: lesson-1.3-queries.sql, P1 to P3.

⬇ Downloads
← Previous
Next →