CREATE SEMANTIC VIEW

statementsupportedAI & ML

docs.snowflake.com ↗

Object: SEMANTIC VIEW

Creates a new semantic view in the current/specified schema. The semantic view must comply with these validation rules. This command supports the following variants: - CREATE OR ALTER SEMANTIC VIEW: Creates a semantic view if it doesn’t exist or alters an existing semantic view. - DEFINE SEMANTIC VIEW: Declarative definition of a semantic view’s target state inside a DCM project. See also: ALTER SEMANTIC VIEW , DESCRIBE SEMANTIC VIEW , DROP SEMANTIC VIEW , SHOW SEMANTIC VIEWS , SHOW SEMANTIC DIMENSIONS , SHOW SEMANTIC DIMENSIONS FOR METRIC , SHOW SEMANTIC FACTS , SHOW SEMANTIC METRICS , SYSTEM$CREATE_SEMANTIC_VIEW_FROM_YAML

Syntax

supportedpartialnot supportednot scored

CREATE [ OR REPLACE ] SEMANTIC VIEW [ IF NOT EXISTS ] <name>
  TABLES ( logicalTable [ , ... ] )
  [ RELATIONSHIPS ( relationshipDef [ , ... ] ) ]
  [ FACTS ( factExpression [ , ... ] ) ]
  [ DIMENSIONS ( dimensionExpression [ , ... ] ) ]
  [ METRICS ( { metricExpression | windowFunctionMetricExpression } [ , ... ] ) ]
  [ COMMENT = '<comment_about_semantic_view>' ]
  [ MAX_STALENESS = <integer> ]
  [ AI_SQL_GENERATION '<instructions_for_sql_generation>' ]
  [ AI_QUESTION_CATEGORIZATION '<instructions_for_question_categorization>' ]
  [ AI_VERIFIED_QUERIES ( verifiedQuery [ , ... ] ) ]
  [ [ WITH ] TAG ( <tag_name> = '<tag_value>' [ , <tag_name> = '<tag_value>' , ... ] ) ]
  [ COPY GRANTS ]

For more information, see CREATE OR ALTER SEMANTIC VIEW usage notes and CREATE OR ALTER <object>.

CREATE OR ALTER SEMANTIC VIEW <name>
  TABLES ( logicalTable [ , ... ] )
  [ RELATIONSHIPS ( relationshipDef [ , ... ] ) ]
  [ FACTS ( factExpression [ , ... ] ) ]
  [ DIMENSIONS ( dimensionExpression [ , ... ] ) ]
  [ METRICS ( { metricExpression | windowFunctionMetricExpression } [ , ... ] ) ]
  [ COMMENT = '<comment_about_semantic_view>' ]
  [ MAX_STALENESS = <integer> ]
  [ AI_SQL_GENERATION '<instructions_for_sql_generation>' ]
  [ AI_QUESTION_CATEGORIZATION '<instructions_for_question_categorization>' ]
  [ AI_VERIFIED_QUERIES ( verifiedQuery [ , ... ] ) ]

Parameters

NameRequiredDescription
namerequiredSpecifies the name of the semantic view; the name must be unique for the schema in which the semantic view is created. In addition, the identifier must start with an alphabetic character and cannot contain spaces or special characters unless the entire identifier string is enclosed in double quotes (for example, "My object"). Identifiers enclosed in double quotes are also case-sensitive. For more information, see Identifier requirements.
COMMENT = 'comment_about_semantic_view'optionalSpecifies a comment about the semantic view.
MAX_STALENESS = integeroptionalSets the maximum number of seconds that a materialization can be stale before Snowflake automatically suspends it. Required before you can add materializations to the semantic view. Minimum value: 120 (2 minutes). Can’t be unset while materializations exist on the semantic view.
AI_SQL_GENERATION 'instructions_for_sql_generation'optionalSpecifies instructions for Cortex Analyst that explain how to generate the SQL statement. For more information, see Providing custom instructions for Cortex Analyst.
AI_QUESTION_CATEGORIZATION 'instructions_for_question_categorization'optionalSpecifies instructions for Cortex Analyst that explain how to classify questions. For more information, see Providing custom instructions for Cortex Analyst.
TAG ( tag_name = 'tag_value' [ , tag_name = 'tag_value' , ... ] )optionalSpecifies the tag name and the tag string value. The tag value is always a string, and the maximum number of characters for the tag value is 256. For information about specifying tags in a statement, see Tag quotas.
COPY GRANTSoptionalWhen you specify OR REPLACE to replace an existing semantic view with a new semantic view, you can set this parameter to copy any privileges granted on the existing semantic view to the new semantic view. The command copies all privilege grants except OWNERSHIP from the existing semantic view to the new semantic view. The role that executes the CREATE SEMANTIC VIEW statement owns the new view. The new semantic view does not inherit any future grants defined for the object type in the schema. The operation to copy grants occurs atomically with the CREATE SEMANTIC VIEW statement (in other words, within the same transaction). If you omit COPY GRANTS, the new semantic view does not inherit any explicit access privileges granted on the existing semantic view but does inherit any future grants defined for the object type in the schema.
table_alias ASSpecifies an optional alias for the logical table. If you specify an alias, you must use this alias when referring to the logical table in relationships, facts, dimensions, and metrics. If you do not specify an alias, you use the unqualified logical table name to refer to the table.
table_nameSpecifies the name of the logical table.
PRIMARY KEY ( primary_key_column_name [ , ... ] )Specifies the names of one or more columns in the logical table that serve as the primary key of the table.
UNIQUE ( unique_column_name [ , ... ] )Specifies the name of a column containing a unique value or the names of columns that contain unique combinations of values. For example, if the column service_id contains unique values, specify:Copy code TABLES( ... product_table UNIQUE (service_id) If the combination of values in the product_area_id and product_id columns is unique, specify:Copy code TABLES( ... product_table UNIQUE (product_area_id, product_id) ... You can identify multiple columns and multiple combinations of columns as unique in a given logical table:Copy code TABLES( ... product_table UNIQUE (product_area_id, product_id) UNIQUE (service_id) ... Note If you already identified a column as a primary key column (by using PRIMARY KEY), do not add the UNIQUE clause for that column.
constraint_nameSpecifies an optional name for the constraint. If you omit this name, the command uses a system-generated name for the constraint.
WITH SYNONYMS [ = ] ( 'synonym' [ , ... ] )Specifies one or more synonyms for the logical table. Unlike aliases, synonyms are used for informational purposes only. You do not use synonyms to refer to the logical table in relationships, dimensions, metrics, and facts.
COMMENT = 'comment_about_table'Specifies a comment about the logical table.
TAG ( tag_name = 'tag_value' [ , tag_name = 'tag_value' , ... ] )Specifies the tag name and the tag string value. The tag value is always a string, and the maximum number of characters for the tag value is 256. For information about specifying tags in a statement, see Tag quotas.
relationship_identifier ASSpecifies an optional identifier for the relationship.
table_alias ( column_name [ , ... ] )Specifies one of the logical tables and one or more of its columns that refers to columns in another logical table.
ref_table_alias [ ( ... ) ]Specifies the other logical table referred to by the first logical table. You can specify one of the following in parentheses, depending on how you want to join the tables: BETWEEN start_column AND end_column EXCLUSIVE Preview Feature — Open Available to all accounts. For a range join, specifies the range of possible values in the first table.
  • ref_column_name [ , ... ] — Specifies a column identified with the PRIMARY KEY or UNIQUE constraint in the logical table definition. If the columns aren’t identified with the PRIMARY KEY or UNIQUE constraint, the relationship is a direct many-to-many relationship (Public Preview).
ASOF ref_column_name [ , ... ]For an ASOF join, specifies a column of one of the supported types. Note You can specify at most one ASOF keyword in the definition of a given relationship. You can specify this keyword before any column in the list.
start_column end_columnSpecifies the columns that define the start and end of the range. You must define a constraint for these columns. You cannot use the same column for both start_column and end_column. If you want to use the same column, use an ASOF relationship. Note column_name must have a data type that can be coerced to the data types for start_column and end_column.
{ PRIVATE | PUBLIC }Specifies whether a fact or metric is private or public. Facts and metrics that are marked as private cannot be queried or used in a query condition. Note You cannot mark a dimension as private. Dimensions are always public. For a dimension, the effect is the same whether you specify or omit PUBLIC. If you omit PRIVATE and PUBLIC, the dimension, fact, or metric is public by default.
table_alias.semantic_expression_nameSpecifies a name for a dimension, fact, or metric.
NON ADDITIVE BY ( dimension [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ] [ , ... ] )Specifies a list of dimensions that should not be used when summing the metric. Instead, during query processing, the rows are sorted by the non-additive dimensions, and the values from the last rows in the sorted order are aggregated to compute the metric. With the default ascending sort order (ASC), the last value is the latest value for time-based dimensions. With descending sort order (DESC), the last value is the earliest value. Specifying the NON ADDITIVE BY clause makes the metric a semi-additive metric. For information, see Identifying the dimensions that should be non-additive for a metric.
  • { ASC | DESC } — Optionally sorts the values of the non-additive dimensions in ascending (lowest to highest) or descending (highest to lowest) order. The engine always retrieves the last value in the sorted order, so ASC returns the latest snapshot and DESC returns the earliest snapshot for time-based dimensions. Default: ASC
  • NULLS { FIRST | LAST } — Optionally specifies whether NULL values are sorted before/after non-NULL values, based on the sort order (ASC or DESC). The sort order determines what the last snapshot is. Default: Depends on the sort order (ASC or DESC); see the usage notes in the ORDER BY documentation.
LABELS = ( FILTER )Specifies that the fact or dimension can also be used as a filter in the WHERE clause of a query. The SQL expression for the fact or dimension must resolve to a BOOLEAN value. You can only specify this parameter for facts and dimensions (not for metrics). For more information, see Defining a filter.
AS sql_exprSpecifies the SQL expression for computing the dimension, fact, or metric. Preview Feature — Open Available to all accounts. For a level of detail (LOD) expression, the sql_expr combines an aggregate function with a FIXED, INCLUDE, or EXCLUDE DIMENSIONS clause. This is a row-level expression, for example COUNT(orders.order_id) FIXED DIMENSIONS (customers.customer_id). To define an LOD metric, wrap the expression in another aggregate function, for example AVG(COUNT(orders.order_id) FIXED DIMENSIONS (customers.customer_id)). For details, see Defining level of detail (LOD) metrics. See Defining facts, dimensions, and metrics. For the validation rules for these expressions, see How Snowflake validates semantic views.
WITH SYNONYMS [ = ] ( 'synonym' [ , ... ] )Specifies one or more optional synonyms for the dimension, fact, or metric. Note that synonyms are used for informational purposes only. You cannot use a synonym to refer to a dimension, fact, or metric in another dimension, fact, or metric.
TAG ( tag_name = 'tag_value' [ , tag_name = 'tag_value' , ... ] )Specifies the tag name and the tag string value. The tag value is always a string, and the maximum number of characters for the tag value is 256. For information about specifying tags in a statement, see Tag quotas.
COMMENT = 'comment_about_dim_fact_or_metric'Specifies an optional comment about the dimension, fact, or metric.
WITH CORTEX SEARCH SERVICE search_service_name [ USING search_service_column_name ]Specifies the Cortex Search Service to use for this dimension. You can only specify this parameter for dimensions (and not for facts or metrics). If the Cortex Search Service is in a different database or schema, qualify the name of the service (for example, my_db.my_schema.my_service). You can set the optional USING clause to the name of the column in the Cortex Search Service.
metricSpecifies a metric expression for this window function. You can specify a metric or any valid metric expression that you can use to define a metric in this entity.
PARTITION BY ...Groups rows into partitions. You can either partition by a specified set of expressions or by all dimensions (except selected dimensions) specified in the query:
  • PARTITION BY exprs_using_dimensions_or_metrics — Groups rows into partitions by SQL expressions. In the SQL expression: Any dimensions in the expression must be accessible from the same entity that defines the window function metric. Any metrics must belong to the same table where this metric is being defined. You cannot specify aggregates, window functions, or subqueries.
  • PARTITION BY EXCLUDING dimensions — Groups rows into partitions by all of the dimensions specified in the SEMANTIC_VIEW clause of the query, except for the dimensions specified by dimensions. dimensions must only refer to dimensions that are accessible from the entity that defines the window function metric. For example, suppose that you exclude the dimension table_1.dimension_1 from partitioning:Copy code CREATE SEMANTIC VIEW sv ... METRICS ( table_1.metric_2 AS SUM(table_1.metric_1) OVER (PARTITION BY EXCLUDING table_1.dimension_1 ORDER BY table_1.dimension_2) ) ... Suppose that you run a query that specifies the dimension table_1.dimension_1:Copy code SELECT * FROM SEMANTIC VIEW( sv METRICS ( table_1.metric_2 ) DIMENSIONS ( table_1.dimension_1, table_1.dimension_2, table_1.dimension_3 ) ); In the query, the metric table_1.metric_2 is evaluated as:Copy code SUM(table_1.metric_1) OVER ( PARTITION BY table_1.dimension_2, table_1.dimension_3 ORDER BY table_1.dimension_2 ) Note how table_1.dimension_1 is excluded from the PARTITION BY clause. Note You cannot use EXCLUDING outside of metric definitions in semantic views. EXCLUDING is not supported in window function calls in any other context.
ORDER BY exprs_using_dimensions_or_metrics [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ... ]Orders rows within each partition. In the SQL expression: Any dimensions in the expression must be accessible from the same entity that defines the window function metric. Any metrics must belong to the same table where this metric is being defined. You cannot specify aggregates, window functions, or subqueries.
windowFrameClauseSee Window function syntax and usage.
verified_query_nameSpecifies an identifier for the verified query.
QUESTION 'question'Specifies the natural language question that the query answers. The question doesn’t need to be a complete sentence or in the form of a question. However, the question should reflect something that a user might ask.
VERIFIED_AT timestampSpecifies the date and time when the query was verified. For timestamp, specify the number of seconds since the beginning of the Unix epoch (midnight on January 1, 1970).
ONBOARDING_QUESTION booleanIf TRUE, specifies that this question will be used as an onboarding question to help users get started. Default: FALSE
VERIFIED_BY '( purpose = contact )'Specifies the contact who verified the query. For purpose, specify one of the predefined purposes.
SQL 'verified_query'Specifies the SQL query that returns the answer for the question.

Usage notes

Access control requirements

A role used to execute this operation must have the following privileges at a minimum: Privilege | Object | Notes CREATE SEMANTIC VIEW | Schema | Required to create a new semantic view. OWNERSHIP | Semantic view | Required to execute a CREATE OR ALTER statement for an existing semantic view. SELECT | Table, view | Required on any tables and/or views used in the semantic view definition. Operating on an object in a schema requires at least one privilege on the parent database and at least one privilege on the parent schema. For instructions on creating a custom role with a specified set of privileges, see Creating custom roles. For general information about roles and privilege grants for performing SQL actions on securable objects, see Overview of Access Control.

CREATE OR ALTER SEMANTIC VIEW usage notes

- All general usage notes for the CREATE OR ALTER command apply. - This command doesn’t support adding or changing tags on the semantic view or on tables, facts, dimensions, or metrics within the semantic view. Any existing tags are preserved. - If a previously set property (for example, COMMENT) is absent in the CREATE OR ALTER SEMANTIC VIEW statement, the property is unset. - To execute a CREATE OR ALTER SEMANTIC VIEW statement for an existing semantic view, a role must have the OWNERSHIP privilege on the semantic view.

Referenced by