CREATE SEMANTIC VIEW
statementsupportedAI & MLCreates 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
| Name | Required | Description |
|---|---|---|
name | required | Specifies 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' | optional | Specifies a comment about the semantic view. |
MAX_STALENESS = integer | optional | Sets 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' | optional | Specifies 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' | optional | Specifies 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' , ... ] ) | optional | 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. |
COPY GRANTS | optional | When 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 AS | Specifies 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_name | Specifies 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_name | Specifies 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 AS | Specifies 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.
| |
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_column | Specifies 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_name | Specifies 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.
| |
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_expr | Specifies 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. | |
metric | Specifies 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:
| |
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. | |
windowFrameClause | See Window function syntax and usage. | |
verified_query_name | Specifies 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 timestamp | Specifies 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 boolean | If 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
- The semantic view must be valid and must follow the rules described in How Snowflake validates semantic views.
- Regarding metadata: Attention Customers should ensure that no personal data (other than for a User object), sensitive data, export-controlled data, or other regulated data is entered as metadata when using the Snowflake service. For more information, see Metadata fields in Snowflake.
- CREATE OR REPLACE <object> statements are atomic. That is, when an object is replaced, the old object is deleted and the new object is created in a single transaction.
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.