Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

DA 3.8: Blend sources and build a dataset

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

60 minutes

Two tills, one answer

The made-up shop sells online and at the till. Both exports are clean now. The owner asks one simple thing: "How many sales did we make in total?"

You cannot answer from either file alone. You must put the two together. There are two ways to do that, and picking the wrong one gives a wrong answer.

Key words

WordWhat it meansExample
RequirementWhat the finished data must be able to do or show.One row per sale
DatasetA set of data that is ready to be analysed, with a clear shape.One table of every sale
StackPut the rows of one table under the rows of another. The columns match.Web sales above till sales
JoinAdd columns to a row by matching a key in another table.Add a category to each sale
Lookup tableA small table that holds extra facts about one kind of thing.Each item and its category
KeyThe column used to match rows between two tables.item
NullA cell with no value.A sale with no category
ConsolidateBring data from many places into one.Two sales files into one

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can choose between stacking and joining, check a blend, and build a finished dataset for a requirement.

Starter: last lesson

Have a go

Starter: last lesson

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Two ways to blend

Stack when two tables hold the same kind of rows with the same columns. A web sale and a till sale are both sales. Stack them to get all the sales.

Join when one table has extra facts about the rows of another. A sale names an item, and a lookup table says the item's category. Join them to add a column.

Worked example: which technique?

TechniqueUse it whenThe shop exampleResult
StackSame columns, different rowsWeb sales and till salesMore rows: 3 and 3 give 6
JoinSame rows, extra columnsSales and the products lookupSame rows, one more column

The cells students fill in are left blank in this preview.

Try it: stack the two sources

Code: duckdb
select 'web' as source, ref, sold_on, item, price from web_sales
union all
select 'till', ref, sold_on, item, price from till_sales
order by sold_on, ref;

-- A source column tells you where each row came from.

Try it: join a category on

Code: duckdb
with sales as (
  select 'web' as source, ref, sold_on, item, price from web_sales
  union all
  select 'till', ref, sold_on, item, price from till_sales
)
select s.source, s.ref, s.item, p.category
from sales s
left join products p on s.item = p.item
order by s.ref;

-- Which sale has no category? Why?

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.