Using an SQL query as a logical table in a semantic view¶
You can use an SQL query (rather than a physical table) as the logical table in a semantic view.
Defining a logical table as an SQL query¶
To specify the SQL query, use the AS clause in the definition of the logical table:
Note
- When you specify an SQL query for a logical table, the alias for the logical table is required.
- You can’t use session variables in the query.
- The query that you specify has the same limitations as the query that you specify in the AS clause of the CREATE VIEW command.
For example, suppose that you have two tables for customer information and customer addresses:
If you want to define a logical table that corresponds to an SQL query that joins these two tables, you can specify the SQL query in the AS clause of the logical table definition:
The following statement queries this semantic view:
Specifying a logical table as an SQL query in the YAML specification¶
To define a logical table as an SQL query in the
YAML specification for a semantic view, specify the definition
name/value pair under base_table:
Note
If you specify definition under base_table, you can’t specify database, schema, and table.
Similarly, if you specify database, schema, or tables, you can’t specify definition.
How logical tables as SQL queries appear in DESC SEMANTIC VIEW and Snowflake views¶
The output of the DESCRIBE SEMANTIC VIEW command includes a new DEFINITION property for logical tables
(where object_kind is TABLE).
-
If a logical table is set to a SQL query, the output includes the
DEFINITIONproperty, which is set to the SQL query.The
BASE_TABLE_DATABASE_NAME,BASE_TABLE_SCHEMA_NAME, andBASE_TABLE_NAMEproperties do not appear in the output. -
If a logical table is set to a physical table, the output includes the
BASE_TABLE_DATABASE_NAME,BASE_TABLE_SCHEMA_NAME, andBASE_TABLE_NAMEproperties, which are set to the names of the database containing the physical table, the schema containing the physical table, and the physical table.The
DEFINITIONproperty does not appear in the output.
For example, the following statement prints out the properties of the my_customer_sv semantic view:
The ACCOUNT_USAGE SEMANTIC_TABLES view and
INFO_SCHEMA SEMANTIC_TABLES view now include a definition column at the
end of the list of the current columns. The definition column includes the SQL query for the table: