Lab: nested events to parent and child tables
Keep one table at event grain and another at item grain. Relate them by event_id and verify both.
Understand the idea
Complete the JSON load lesson first. Confirm M7_EVENTS_RAW has exactly two rows with distinct event IDs before creating outputs. These strict casts assume the supplied fixture; real ingestion must reject invalid IDs, non-array items and incompatible item values.
| Concept | What it means |
|---|---|
| M7_EVENT_HEADER | One row per event, including empty events. |
| M7_EVENT_ITEM | One row per actual item. |
| Child identity | Event ID plus item index for this immutable fixture. |
| Reconciliation | 2 parents, 2 children, 3 units, total 35. |
A concrete example
Asha’s event has two items worth 35. Bilal’s event remains present even though it has no items.
Remember: Parent preservation and child expansion are separate requirements.
Explain it step by step · full theory
How to apply it
Complete the JSON load lesson first. Confirm M7_EVENTS_RAW has exactly two rows with distinct event IDs before creating outputs. These strict casts assume the supplied fixture; real ingestion must reject invalid IDs, non-array items and incompatible item values.
CREATE TABLE AS SELECT is a one-time lab snapshot, not an incremental pipeline. Repeating inserts or delivering duplicate events needs an explicit replay/deduplication policy. The uniqueness query below must return no rows; a standard-table key declaration alone does not enforce deduplication.
Use the parent table as the reporting starting point and LEFT JOIN child totals. COALESCE to zero here means an event with a known empty items array has no item value; do not use it to hide an invalid price.
Words used in this lesson
- M7_EVENT_HEADER
- One row per event, including empty events.
- M7_EVENT_ITEM
- One row per actual item.
- Child identity
- Event ID plus item index for this immutable fixture.
- Reconciliation
- 2 parents, 2 children, 3 units, total 35.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Lab: nested events to parent and child tables
Read the shape
Keep one table at event grain and another at item grain. Relate them by event_id and verify both.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Read the shape
Keep one table at event grain and another at item grain. Relate them by event_id and verify both.
- Follow the example
Asha’s event has two items worth 35. Bilal’s event remains present even though it has no items.
- Verify the result
Parent preservation and child expansion are separate requirements.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Manual learning examples; nothing runs on this website. Standalone WITH/SELECT examples supply their own input. Table and file exercises require the first lesson’s L2_OBJECTS_LAB.M7_LAB setup and an authorized warehouse. Select that schema in every session. The final lab requires the supplied events.json to be loaded first.
Check input and create the two grains
SELECT PAYLOAD:event_id::NUMBER AS EVENT_ID, COUNT(*) AS COPIES
FROM M7_EVENTS_RAW GROUP BY 1 HAVING COUNT(*) <> 1;
-- Continue only when the input has exactly the two supplied events.
CREATE TABLE M7_EVENT_HEADER AS
SELECT PAYLOAD:event_id::NUMBER AS EVENT_ID,
PAYLOAD:customer.name::VARCHAR AS CUSTOMER
FROM M7_EVENTS_RAW;
CREATE TABLE M7_EVENT_ITEM AS
SELECT r.PAYLOAD:event_id::NUMBER AS EVENT_ID,
i.INDEX AS ITEM_INDEX, i.VALUE:sku::VARCHAR AS SKU,
i.VALUE:qty::NUMBER AS QTY,
i.VALUE:price::NUMBER(10,2) AS PRICE
FROM M7_EVENTS_RAW r, LATERAL FLATTEN(INPUT=>r.PAYLOAD:items) i;What to expect and why
The duplicate check returns no rows. The header table has 2 rows; the item table has 2 real items and no placeholder for the empty event.
Report without dropping the empty event
SELECT h.EVENT_ID, h.CUSTOMER, COUNT(i.ITEM_INDEX) AS ITEMS,
COALESCE(SUM(i.QTY * i.PRICE), 0) AS EVENT_TOTAL
FROM M7_EVENT_HEADER h LEFT JOIN M7_EVENT_ITEM i
ON h.EVENT_ID=i.EVENT_ID
GROUP BY h.EVENT_ID, h.CUSTOMER ORDER BY h.EVENT_ID;
SELECT COUNT(*) AS ITEM_ROWS, SUM(QTY) AS UNITS,
SUM(QTY * PRICE) AS TOTAL FROM M7_EVENT_ITEM;What to expect and why
Report: (101,Asha,2,35) and (102,Bilal,0,0). Child reconciliation: 2 rows, 3 units, total 35.
Inspect keys and optional cleanup
SELECT EVENT_ID, ITEM_INDEX, COUNT(*) AS COPIES FROM M7_EVENT_ITEM
GROUP BY EVENT_ID, ITEM_INDEX HAVING COUNT(*) > 1;
-- After all Module 07 exercises, optional cleanup of this lab only:
-- DROP SCHEMA L2_OBJECTS_LAB.M7_LAB CASCADE;What to expect and why
No duplicate child keys are expected. Cleanup is commented out because it removes all tables and staged files in this dedicated schema.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Extend the model with item tags without changing the item total.
Compare your reasoning
Create a separate tag table keyed by event_id, item_index and tag_index, using the chained expansion from lesson 5. Keep the item-total calculation on M7_EVENT_ITEM.
Official sources and curriculum
Module 07 · See module scope