Zum Inhalt springen

Lektion 12 · Facts and Dimensions

Special Dimension Patterns

Learn role-playing, degenerate, junk, outrigger and mini-dimensions, plus bridge tables for many-to-many relationships, with runnable SQL examples.

Most dimensions are simple. A few situations come up again and again that need a named pattern. Recognizing them saves you from inventing awkward designs.

Patterns for common problems

Role-playing One dimension, several roles
Degenerate A key with no table
1 / 2

Role-playing dimensions

An order has an order date, a ship date and a delivery date. All three point to the same date dimension, each in a different role. You keep one physical table and join it several times with different aliases, or create one view per role with clear column names such as ship_month.

SQL One date dimension, two roles

Press Run (or Ctrl + Enter) to see the result here.

Always say which role a "monthly sales" report uses, since order month and ship month give different totals.

Degenerate dimensions

A receipt number or order number is useful for grouping the lines of one transaction, but after you have moved its other details into dimensions, nothing is left to describe it. Keep the number directly in the fact table, with no dimension table. This is a degenerate dimension. It lets you count baskets with COUNT(DISTINCT receipt_no).

Junk dimensions

Transactions often come with several small flags: payment method, gift wrap yes or no, order channel. Separate tiny dimensions clutter the model, and loose flag columns bloat the fact table. A junk dimension combines them: one row for each combination that is used, and one key in the fact table.

SQL Build a junk dimension from observed combinations

Press Run (or Ctrl + Enter) to see the result here.

You can also pre-build every possible combination when there are only a few. If the flags have many unrelated values, the combinations can explode, so split them.

Outriggers and mini-dimensions

  • An outrigger is a dimension referenced by another dimension, for example a "first purchase date" in the customer dimension pointing to a date table. Use it sparingly; it is a form of snowflaking.
  • A mini-dimension moves rapidly changing attributes, such as age band and activity score band, out of a large dimension into a small separate one. The fact table gets its own key to it, which records the profile at the time of the event without creating a new customer row for every change. Banding values keeps it small.

Bridge tables for many-to-many

A bank account can have two owners. A hospital visit can have several diagnoses. If each fact can relate to several dimension rows, a bridge table sits between them.

If you simply join and sum, amounts repeat once per related row. One option is a weighting factor that adds up to 1 for each group.

SQL A bridge table with weighting factors

Press Run (or Ctrl + Enter) to see the result here.

The first view totals 2,500 although the accounts hold only 1,500. Both views can be valid; label them clearly. The weights themselves are a business rule, not something SQL can discover.

Bridges also model ragged hierarchies of varying depth, such as organization charts, by listing every ancestor and descendant pair with the distance between them.

Choosing between look-alike patterns

Some situations seem to fit more than one pattern. These questions help:

Situation Likely choice
The same kind of thing appears twice on one fact Role-playing
An identifier has no attributes left Degenerate
A few low-variety flags Junk dimension
A large dimension has attributes that change constantly Mini-dimension
One fact relates to many members of a dimension Bridge table, or a finer grain

Before adding a bridge, ask whether you can change the grain instead. If every diagnosis has its own cost, a fact table with one row per diagnosis may be simpler than a bridge. A bridge is useful when the measurement belongs to the whole fact and only the relationship is multiple.

Bridge tables over time

Relationships change too. An account can gain or lose an owner. If history matters, give each bridge row valid_from and valid_to columns, and include the fact date in the join. Then check that the weights add up to 1 for every group at every point in time, not only across the whole table. A join that ignores the dates would include old and new owners together and inflate the result.

Ragged hierarchies

An organization chart may be two levels deep in one branch and six in another. Fixed columns such as level_1 to level_6 waste space and break when the depth grows. A hierarchy bridge stores one row for every ancestor and descendant pair, including each member paired with itself at distance zero. To total everything under a manager, join facts to the bridge on the descendant and filter on the ancestor. Because each descendant appears once under a given ancestor in a simple tree, that total does not double count. Totals across several ancestors do overlap, so do not add them together.

Explain the pattern to readers

A pattern helps only if the people who query the model understand it. In the table description, say which date plays which role, which column is a degenerate identifier and what each junk dimension combines. For bridges, add a short example query for the allocated view and one for the impact view. Where you can, publish ready-made views with clear names, such as v_balance_allocated_by_owner, so that readers do not have to remember the weighting rule. Then state next to each view whether its rows may be totaled. This short note prevents the most expensive mistake with bridges: presenting repeated amounts as if they were a company total.

Common mistakes

Do not copy a degenerate order number into a huge one-row-per-order dimension that adds joins and nothing else. And never total an unweighted bridge result as though it were real money.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. An order fact has order, ship and delivery date keys to one date table. What is this?

    One dimension plays several roles.
  2. 2. Where does a degenerate dimension such as a receipt number live?

    It has no other attributes, so it stays in the fact table.
  3. 3. What problem does a weighting factor in a bridge table address?

    Weights that sum to 1 per group keep allocated totals equal to the real total.

Summary

  • Role-playing dimensions reuse one table for several meanings.
  • Degenerate dimensions stay in the fact table; junk dimensions group small flags.
  • Outriggers and mini-dimensions handle special sub-relationships and fast-changing attributes.
  • Bridge tables model many-to-many links and require careful aggregation.

Next: Slowly Changing Dimension Types

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.