Module 3 · Data, Variables, Samples, and Measurement Lesson 25 of 120

Identifiers, Keys, Joins, and Data Grain

A duplicate join can double the apparent money.

2:37 clip1:19:01–1:21:39 in the full courseWatch on YouTube

Transcript

22 sentences · select one to jump there

Check your understanding

Is the sum function responsible for this doubled total?

Choose one answer

Code lab

Run it yourself

The lesson source in 7 languages. Edit it, run TypeScript and Python right here, and compare with the expected output.

025-identifiers-keys-joins-and-data-grain.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 025 of 120
 * Identifiers, Keys, Joins, and Data Grain
 * Module 03: Data, Variables, Samples, and Measurement
 *
 * Scenario: A duplicate join can double the apparent money
 * Rule:     join cardinality must match the declared grain
 *
 * Try it:   Is the sum function responsible for this doubled total?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/identifiers-keys-joins-and-data-grain/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson025() {
  const payments = [{id: "P1", customer: "C1", amount: 100}];
  const profiles = [{customer: "C1"}, {customer: "C1"}];
  const matches = payments.flatMap(p =>
    profiles.filter(c => c.customer === p.customer).map(() => p));
  const result = {joinedRows: matches.length,
    apparentTotal: matches.reduce((s,p) => s+p.amount,0)};
  return result;
}

export const checkedResult = {"joinedRows":2,"apparentTotal":200};

// Run this file directly: npx tsx lessons/03-data-variables-samples-and-measurement/025-identifiers-keys-joins-and-data-grain.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson025(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "joinedRows": 2,
  "apparentTotal": 200
}

Prefer your own machine? Every file is in the course repository · open it in Codespaces.

Lesson notes

The rule

join cardinality must match the declared grain