Choosing What One Table Row Represents

Row granularity says what one observation or entity in a table means. Measures and keys depend on that choice.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Compare booking and booking line
  2. Write the grain as a sentence
  3. Write a sentence that can identify every row
  4. Verify the grain after each join and grouping

Compare booking and booking line

A booking row can store an overall status. A booking line can store one item and quantity. Summing a booking total after duplicating it onto each line may inflate the result. See join cardinality.

Write the grain as a sentence

For each table, state “one row per” and then name the identifying fields. Primary keys and display names explain why that choice deserves a stable key.[1][2]

Write a sentence that can identify every row

A table can have one row per order or one row per product line in an order. Summing an order-level fee across line rows repeats it for multi-line orders. Keep order data in the order relation and line-level quantities in the line relation. See aggregating before joining.

Order 5 has shipping 8
Order 5 has three product lines
Direct join repeats shipping 8 three times
Order-level shipping total remains 8

Verify the grain after each join and grouping

A query result can change grain independently from its source tables. State whether a row represents a customer, order, line or group. Test an order with more than one line because a one-line fixture hides the error.

References

  1. PostgreSQL Global Development Group, Data Definition ↩
  2. PostgreSQL Global Development Group, SQL Language Tutorial ↩