SQL

Count each event stream before combining it

BOTIE

Group the visit records into one count row per asset before attaching any reading data.
Ready to test
0 passed · 0 failed · 1 not run

An empty floor. Start with the information your machine needs.

Received result

Your answer will arrive here.

Examples, experiments and workspace controls
Live values

Connect a tool's inputs to see what it produces. You can try a branch before connecting it to Result.

Try your own inputs

Use a copy of the example to try your own data. Keep the problem's assumptions in mind, and predict the effect of a change before checking the output. You can also add another branch to compare two ways of doing the calculation.

Start at an output arrow, then click or drag to an input. Drag empty space to pan. Delete removes a selection. Ctrl/Cmd+Z undoes a change.
Context for this challenge

Group the visit records into one count row per asset before attaching any reading data.

Joining raw visits to raw readings would multiply the event rows for an asset.

Inspect the component calculation
Summarise each group(
  Rows: Inspection event records
  Group field: asset_id
  Value field: inspection_id
  Aggregate: count rows
  Result field: visits
)
Examples and results
Inspection event records
[
  {
    "inspection_id": 101,
    "asset_id": "P1",
    "minutes": 30,
    "finding": "loose"
  },
  {
    "inspection_id": 102,
    "asset_id": "P1",
    "minutes": null,
    "finding": null
  },
  {
    "inspection_id": 103,
    "asset_id": "P2",
    "minutes": 0,
    "finding": "clear"
  },
  {
    "inspection_id": 104,
    "asset_id": "P3",
    "minutes": 20,
    "finding": "clear"
  },
  {
    "inspection_id": 105,
    "asset_id": "P2",
    "minutes": 10,
    "finding": "loose"
  },
  {
    "inspection_id": 106,
    "asset_id": "P1",
    "minutes": 15,
    "finding": "loose"
  }
]
Required result
[
  {
    "asset_id": "P1",
    "visits": 3
  },
  {
    "asset_id": "P2",
    "visits": 2
  },
  {
    "asset_id": "P3",
    "visits": 1
  }
]
Try a fresh construction

This clears this draft, including its undo history. Earlier successful attempts stay recorded. Build again without opening hints to record an independent attempt.

Stuck? Inspect a worked construction

The tools below form one possible solution. You can assemble it, follow its signals, and undo that change to return to your own construction.

Assumptions and test precision

The workshop calculates over small record lists to make the relational operations visible. It is not a SQL parser or query planner. The worksheet runs SQLite and is the authority for SQL behaviour, including types and NULL. Numerical tests allow a difference of 0.001. Lists and structures must also have the right shape.

Your last tested pieces can start the full construction. An existing draft there keeps its own pieces.

One asset is present in only one stream

This check uses the pieces from “Join summaries at the intended grain”.

BOTIE

P8 has two visits and one reading; P9 has one visit but no reading. Predict this workshop’s inner-joined summaries. The later SQL challenge uses the register to retain every asset.
Inspection event records
[
  {
    "asset_id": "P8",
    "inspection_id": 1
  },
  {
    "asset_id": "P9",
    "inspection_id": 2
  },
  {
    "asset_id": "P8",
    "inspection_id": 3
  }
]
Reading event records
[
  {
    "asset_id": "P8",
    "reading_id": 1
  }
]

Use a number, text, or an array matching the requested result. For a matrix, put each row in brackets. Saving your prediction runs your current construction on these inputs and checks the earlier examples too.

Build a construction in the workspace above first. This case will test those pieces.

Why can SUM(DISTINCT minutes) lose legitimate work when two different visits took the same time?

Deduplicate event identities when that is the question; equal numeric values do not identify equal events.

The workshop calculates over small record lists to make the relational operations visible. It is not a SQL parser or query planner. The worksheet runs SQLite and is the authority for SQL behaviour, including types and NULL.

Work through the SQL challenges →
Continue the courseWhich Inspections Took Longer Than the Usual Visit? →
← Which Assets Have No Matching Inspection?