DuckDB Semantic Views
  • Overview
  • Tutorials
    • Getting Started Install the extension and run your first query in 5 minutes
    • Multi-Table Semantic Views Model multi-table schemas with relationships
    • Building a Model Build a realistic model with facts and derived metrics
  • How-To Guides
    • FACTS Define reusable row-level expressions referenced in metrics
    • Derived Metrics Compose metrics from other metrics using arithmetic
    • Role-Playing Dimensions Join the same table multiple times with distinct aliases
    • Fan Traps How a metric below its grain is counted, and when a fan trap is refused
    • ASOF and Range Relationships Join each row to the record in effect at its date, or to the period holding it
    • Many-to-Many Relationships Group a metric across a bridge table or a non-key column, each row counted once per group
    • Dimension References Write a dimension as a dimension of a table its table references
    • Tables Defined by a Query Join, filter or pre-aggregate source tables inside the view's TABLES clause
    • Data Sources Connect CSV, Parquet, Iceberg, and database tables to semantic views
    • Filtering Filter before aggregation inside SEMANTIC_VIEW(...) or after it with an outer WHERE
    • View Variables Set thresholds, rates and reporting dates per query with VARIABLES
    • Standard SQL Query a view by name with SELECT ... GROUP BY and AGG()
    • Metadata Annotations Add comments, synonyms, and access modifiers to view definitions
    • Semi-Additive Metrics Define snapshot metrics with NON ADDITIVE BY for balances and inventory
    • Window Metrics Define rolling averages, lag comparisons, and rankings with OVER clauses
    • LOD Expressions Compute a value at another grain with FIXED, INCLUDE or EXCLUDE DIMENSIONS
    • Wildcard Selection Select all items for a table alias using alias.* patterns
    • Query Facts Query facts directly as row-level columns without aggregation
    • Materializations Route matching queries to pre-aggregated tables
    • YAML Definitions Export and import semantic view definitions as YAML
    • CREATE OR ALTER Change a view in place, keeping its creation time, tags and valid materializations
  • Explanation
    • Semantic Views vs Regular Views What semantic views add beyond standard SQL views
    • Metric Grain Why each metric aggregates at its own grain and how multi-grain queries are assembled
    • Snowflake Comparison Feature comparison with Snowflake SQL DDL semantic views
    • Databricks Comparison Feature comparison with Databricks metric views
    • Transactional DDL and Limitations How DDL participates in transactions, plus read-only and concurrency caveats
  • Reference
  • Release notes
/
  • Tutorials
    • Getting Started
    • Multi-Table Semantic Views
    • Building a Complete Model
  • How-To Guides
    • How to Use FACTS for Reusable Row-Level Logic
    • How to Compose Metrics with Derived Metrics
    • How to Model Role-Playing Dimensions
    • How to Diagnose and Fix Fan Traps
    • How to Join on Dates with ASOF and Range Relationships
    • How to Model Many-to-Many Relationships
    • How to Reuse a Dimension of a Related Table
    • How to Define a Logical Table by a Query
    • How to Use Different Data Sources
    • How to Filter Semantic View Queries
    • How to Parameterize a View with Variables
    • How to Query a Semantic View with Standard SQL
    • How to Use Metadata Annotations
    • How to Use Semi-Additive Metrics
    • How to Use Window Function Metrics
    • How to Compute Values at Another Grain (LOD Expressions)
    • How to Use Wildcard Selection
    • How to Query Facts Directly
    • How to Use Materializations
    • How to Import and Export YAML Definitions
    • How to Change a View in Place with CREATE OR ALTER
  • Explanation
    • Semantic Views vs. Regular SQL Views
    • Metric Grain and How Queries Are Assembled
    • Snowflake Comparison
    • Databricks Comparison
    • Transactional DDL and Known Limitations
  • Reference
    • CREATE SEMANTIC VIEW
    • ALTER SEMANTIC VIEW
    • DROP SEMANTIC VIEW
    • DESCRIBE SEMANTIC VIEW
    • SHOW SEMANTIC VIEWS
    • SHOW SEMANTIC DIMENSIONS
    • SHOW SEMANTIC METRICS
    • SHOW SEMANTIC FACTS
    • SHOW SEMANTIC MATERIALIZATIONS
    • SHOW SEMANTIC DIMENSIONS FOR METRIC
    • SHOW COLUMNS IN SEMANTIC VIEW
    • GET_DDL
    • READ_YAML_FROM_SEMANTIC_VIEW
    • YAML Definition Format
    • SEMANTIC_VIEW(…) and semantic_view()
    • explain_semantic_view()
    • Functions
    • Error Messages
  • Release notes

On this page

  • Set Up Example Tables
  • Reference Another Table’s Dimension
  • Group and Filter by a Reference
  • What Is Refused
  • Related
  1. DuckDB Semantic Views /
  2. How-To Guides /
  3. How to Reuse a Dimension of a Related Table

How to Reuse a Dimension of a Related Table¶

Added in version 0.20.0.

This guide shows how to define a dimension as a dimension of another table, such as each order’s region read through its customer. Snowflake calls this a reference to another logical table’s dimension. The expression is written once, on the table that holds it, and every dimension that references it reads the same value along its own relationships.

Prerequisites:

  • A working multi-table semantic view (see Multi-Table Semantic Views)

  • Familiarity with the RELATIONSHIPS clause (see CREATE SEMANTIC VIEW)

Set Up Example Tables¶

Two regions, three customers (Cid has no region) and four orders:

CREATE TABLE ref_regions (region_id INTEGER, region_name VARCHAR);
INSERT INTO ref_regions VALUES (1, 'Europe'), (2, 'Americas');

CREATE TABLE ref_customers (customer_id INTEGER, name VARCHAR, region_id INTEGER);
INSERT INTO ref_customers VALUES (10, 'Ann', 1), (20, 'Bob', 2), (30, 'Cid', NULL);

CREATE TABLE ref_orders (order_id INTEGER, customer_id INTEGER, amount DECIMAL(10,2));
INSERT INTO ref_orders VALUES (1, 10, 30), (2, 10, 20), (3, 20, 50), (4, 30, 15);

Reference Another Table’s Dimension¶

Write the other table’s dimension, <alias>.<dimension name>, where a column expression would go. customer.customer_region reads the region’s region_name dimension, and orders.order_region reads customer.customer_region in turn. A reference can also sit inside an expression, as in region_label:

CREATE SEMANTIC VIEW regional_orders
TABLES (
    region   AS ref_regions   PRIMARY KEY (region_id),
    customer AS ref_customers PRIMARY KEY (customer_id),
    orders   AS ref_orders    PRIMARY KEY (order_id)
)
RELATIONSHIPS (
    customer_region AS customer(region_id) REFERENCES region,
    order_customer  AS orders(customer_id) REFERENCES customer
)
DIMENSIONS (
    region.region_name       AS region_name,
    customer.customer_region AS region.region_name,
    orders.order_region      AS customer.customer_region,
    customer.region_label    AS UPPER(region.region_name),
    customer.customer_name   AS name
)
METRICS (
    orders.revenue AS SUM(amount)
);

Each customer reads the name of its own region. Cid, who has none, gets NULL:

SELECT * FROM SEMANTIC_VIEW(regional_orders
    DIMENSIONS customer.customer_name, customer.customer_region, customer.region_label
) ORDER BY 1;
┌───────────────┬─────────────────┬──────────────┐
│ customer_name │ customer_region │ region_label │
├───────────────┼─────────────────┼──────────────┤
│ Ann           │ Europe          │ EUROPE       │
│ Bob           │ Americas        │ AMERICAS     │
│ Cid           │ NULL            │ NULL         │
└───────────────┴─────────────────┴──────────────┘

Group and Filter by a Reference¶

A reference is queried like any dimension. orders.order_region groups each order by its customer’s region, two relationships away:

SELECT * FROM SEMANTIC_VIEW(regional_orders
    DIMENSIONS orders.order_region
    METRICS orders.revenue
) ORDER BY 1 NULLS LAST;
┌──────────────┬───────────────┐
│ order_region │    revenue    │
├──────────────┼───────────────┤
│ Americas     │         50.00 │
│ Europe       │         50.00 │
│ NULL         │         15.00 │
└──────────────┴───────────────┘

A filter works the same way: WHERE orders.order_region = 'Europe' keeps Ann’s two orders, 50.00. DESCRIBE SEMANTIC VIEW, GET_DDL and the YAML export show a reference as written, region.region_name.

Tip

A reference is read along the referencing dimension’s own path, whatever the query’s metrics are. If orders also reached regions through a store, customer.customer_region would still be the customer’s region, also for a metric whose USING names the store relationship.

What Is Refused¶

CREATE refuses, as Snowflake does:

  • A reference to a table that references the dimension’s table. A customer has several orders, so an order dimension has no single value per customer:

    Parser Error: dimension 'customer.order_amount' references dimension 'orders.amount_d', but table 'customer' does not reach table 'orders' along its relationships, so 'amount_d' has no single value per row of 'customer'. As in Snowflake, a dimension can reference a dimension of a table it references, directly or through other tables, not of a table that references it.
    
  • A reference to a table reached by more than one path, such as a role-played table, since the path is not said.

  • References that form a cycle.

See dimension references for the full rules.

Related¶

  • Dimension references – The rules in the CREATE SEMANTIC VIEW reference

  • How to Use FACTS for Reusable Row-Level Logic – Reusable row-level expressions on one table

  • How to Model Role-Playing Dimensions – A table referenced more than once

Previous
How to Model Many-to-Many Relationships
Next
How to Define a Logical Table by a Query

2026, Anentropic

Made with Sphinx and Shibuya theme.