Specifying the SEMANTIC_VIEW clause in the FROM clause¶
To query a semantic view, you can specify the SEMANTIC_VIEW clause in the FROM
clause.
The following example selects the customer_market_segment dimension and the order_average_value metric from the
tpch_analysis semantic view, which you defined earlier:
Internally, this statement is rewritten as a SELECT statement that uses the
SEMANTIC_VIEW clause:
The expressions that you specify in the GROUP BY clause are rewritten into the DIMENSIONS clause in the SEMANTIC_VIEW clause.
In the SELECT statement, if you use an expression that is not in the GROUP BY clause (for example, a dimension
expression in the SELECT list), the rewrite uses that expression in the FACTS clause in the SEMANTIC_VIEW clause.
When you refer to a metric that is defined in a semantic view, you must pass the metric to the AGG function.
You can select ad-hoc metrics by passing a dimension or fact to any
aggregate function.
Any other calculated values that don't fall into the first two categories are considered to be fact references.
The next sections explain these requirements in more detail:
Requirements for dimensions and metrics in a SELECT statement¶
In the SELECT statement, you can only refer to dimensions and metrics that have distinct names and that are not distinguished by
their logical table name. For example, suppose that a semantic view has two dimensions that have the unqualified name name:
In the SELECT statement, when you specify the qualified name of a dimension or metric, the qualifier is interpreted as the name
of the semantic view, not the name of a logical table:
If you want to select a metric that is defined in a semantic view, you must pass the metric to the
AGG function, which is a special aggregate function for metrics in semantic views.
For example:
SELECTAGG(order_average_value)FROMtpch_analysis;
备注
The AGG function has no effect on the metric because the function evaluates one value of the metric.
In the SELECT list, you can specify an expression that uses a metric. For example:
For example, suppose that you query the tpch_analysis semantic view that you created in 使用 SQL 创建语义视图的示例, and you want to return
the orders.order_date dimension and the customer.customer_order_count metric:
010234 (42601): SQL compilation error:Invalid dimension specified: The dimension entity 'ORDERS' must be related to and have an equal or lower level of granularity compared to the base metric or dimension entity 'CUSTOMER'.
+----------------+-------------+| NAME | NAME |+----------------+-------------+| BRAZIL | AMERICA || MOROCCO | AFRICA || UNITED KINGDOM | EUROPE || IRAN | MIDDLE EAST || FRANCE | EUROPE || ... | ... |+----------------+-------------+
+----------------+-------------+| NATION_NAME | REGION_NAME |+----------------+-------------+| BRAZIL | AMERICA || MOROCCO | AFRICA || UNITED KINGDOM | EUROPE || IRAN | MIDDLE EAST || FRANCE | EUROPE || ... | ... |+----------------+-------------+
CREATEORREPLACESEMANTIC VIEWsv_window_function_exampleTABLES(store_salesASSNOWFLAKE_SAMPLE_DATA.TPCDS_SF10TCL.store_sales,dateASSNOWFLAKE_SAMPLE_DATA.TPCDS_SF10TCL.date_dimPRIMARY KEY(d_date_sk))RELATIONSHIPS(sales_to_dateASstore_sales(ss_sold_date_sk)REFERENCESdate(d_date_sk))DIMENSIONS(date.dateASd_date,date.d_date_skASd_date_sk,date.yearASd_year)METRICS(store_sales.total_sales_quantityASSUM(ss_quantity)WITH SYNONYMS=('Total sales quantity'),store_sales.avg_7_days_sales_quantityasAVG(total_sales_quantity)OVER(PARTITIONBYEXCLUDINGdate.date,date.yearORDERBYdate.dateRANGEBETWEENINTERVAL'6 days'PRECEDINGANDCURRENTROW)WITH SYNONYMS=('Running 7-day average of total sales quantity'),store_sales.total_sales_quantity_30_days_agoASLAG(total_sales_quantity,30)OVER(PARTITIONBYEXCLUDINGdate.date,date.yearORDERBYdate.date)WITH SYNONYMS=('Sales quantity 30 days ago'),store_sales.avg_7_days_sales_quantity_30_days_agoASAVG(total_sales_quantity)OVER(PARTITIONBYEXCLUDINGdate.date,date.yearORDERBYdate.dateRANGEBETWEENINTERVAL'36 days'PRECEDINGANDINTERVAL'30 days'PRECEDING)WITH SYNONYMS=('Running 7-day average of total sales quantity 30 days ago'));
当您查询语义视图且查询返回窗口函数指标时,必须同时返回在 CREATE SEMANTIC VIEW 语句中为该语义视图的 PARTITION BY dimension、PARTITION BY EXCLUDING dimension 和 ORDER BY dimension 指定的维度。
例如,假设您在 store_sales.avg_7_days_sales_quantity 指标的定义中,在 PARTITION BY EXCLUDING 和 ORDER BY 子句中指定了``date.date`` 和 date.year 维度:
CREATEORREPLACESEMANTIC VIEWsv_window_function_example...DIMENSIONS(...date.dateASd_date,...date.yearASd_year...)METRICS(...store_sales.avg_7_days_sales_quantityasAVG(total_sales_quantity)OVER(PARTITIONBYEXCLUDINGdate.date,date.yearORDERBYdate.dateRANGEBETWEENINTERVAL'6 days'PRECEDINGANDCURRENTROW)WITH SYNONYMS=('Running 7-day average of total sales quantity'),...);
010260 (42601): SQL compilation error:Invalid semantic view query: Dimension 'DATE.DATE' used in a window function metric must be requested in the query.