查询语义视图
To query a semantic view, you can use a standard SELECT statement. Within this statement, you can use one of the following approaches:
-
Specify the SEMANTIC_VIEW clause in the FROM clause. For example:
For information, see Specifying the SEMANTIC_VIEW clause in the FROM clause.
-
Specify the name of the semantic view in the FROM clause. For example:
For information, see Specifying the name of the semantic view in the FROM clause.
查询语义视图所需的权限
如果您使用的角色不拥有语义视图,则您必须获得授予该语义视图的 SELECT 权限才能查询该语义视图。
Note
To query a semantic view, you don’t need the SELECT privilege on the tables used in the semantic view. You only need the SELECT privilege on the semantic view itself.
This behavior is consistent with the privileges required to query standard views.
For information about granting privileges on semantic views, see Granting privileges on semantic views.
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:
Note that you can define an alias for a dimension or metric by specifying the alias after the dimension or metric name. You can
also specify the optional keyword AS before the alias. The following example runs the same query but uses the aliases segment
and average for the dimension and metric returned in the results.
The following example selects the customer_name dimension and the c_customer_order_count fact from the
tpch_analysis semantic view:
Guidelines for specifying the SEMANTIC_ VIEW clause¶
When specifying the SEMANTIC_VIEW clause, follow these guidelines:
-
In the SEMANTIC_VIEW clause, you must specify at least one of the following clauses:
- METRICS
- DIMENSIONS
- FACTS
You cannot omit all of these clauses from the SEMANTIC_VIEW clause.
-
When specifying a combination of these clauses, note the following:
-
You cannot specify FACTS and METRICS in the same SEMANTIC_VIEW clause.
-
Although you can specify both FACTS and DIMENSIONS in a query, you should do so only if the dimensions can uniquely determine the facts.
The query groups the results by dimensions. if the facts do not depend on the dimensions, the results can be non-deterministic.
-
If you specify both FACTS and DIMENSIONS, all facts and dimensions used in the query (including those specified in the WHERE clause) must be defined in the same logical table.
-
If you specify a dimension and a metric, the logical table for the dimension must be related to the logical table for the metric.
In addition, the logical table for the dimension must have an equal or lower level of granularity than the logical table for the metric.
To determine which dimensions meet this criteria, you can run the SHOW SEMANTIC DIMENSIONS FOR METRIC command.
For details, see Choosing the dimensions that you can return for a given metric.
-
-
In the DIMENSIONS clause, you can specify an expression that refers to a fact. Similarly, in the FACTS clause, you can specify an expression that refers to a dimension. For example:
One of the main differences between using DIMENSIONS and FACTS is that the query groups the results by the dimensions and expressions specified in the DIMENSIONS clause.
-
In the METRICS clause, you can specify an expression that includes:
- A scalar expression referring to metrics.
- An aggregation of dimensions or facts.
-
Specify the METRICS, DIMENSIONS, and FACTS clauses in the order in which you want them to appear in the results.
If you want the dimensions to appear first in the results, specify DIMENSIONS before METRICS. Otherwise, specify METRICS first.
For example, suppose that you specify the METRICS clause first:
In the output, the first column is the metric column (
customer_order_count) and the second column is the dimension column (customer_name):If you instead specify the DIMENSIONS clause first:
In the output, the first column is the dimension column (
customer_name) and the second column is the metric column (customer_order_count): -
You can use the relation defined by a SEMANTIC_VIEW clause in other SQL constructs, including JOIN, PIVOT, UNPIVOT, GROUP BY, and common table expressions (CTEs).
-
The output column headers use the unqualified names of the metrics and dimensions.
If you have multiple metrics and dimensions with the same names, use a table alias to assign different names to the column headers. See Handling duplicate column names in the output.
To return all metrics or dimensions in a given logical table, use an asterisk as a wildcard, qualified by the name of the logical
table. For example, to return all metrics and dimensions defined in the customer logical table:
Examples of specifying the SEMANTIC_ VIEW clause¶
The following examples use the tpch_analysis view defined in Example of using SQL to create a semantic view:
- Retrieving a metric
- Grouping metric data by a dimension
- Using the SEMANTIC_VIEW subclause with other constructs
- Specifying scalar expressions that use dimensions
- Specifying the WHERE clause
- Specifying facts in the WHERE clause
检索指标
以下语句通过查询指标来检索客户总数:
按维度对指标数据进行分组
The following statement groups metric data (order_average_value) by a dimension (customer_market_segment):
将 SEMANTIC_ VIEW 分子句与其他结构一起使用¶
以下示例演示了如何在 SEMANTIC_VIEW 分子句中使用维度和指标,并结合其他 SQL 结构来筛选、排序和限制结果:
指定使用维度的标量表达式
以下示例使用标量表达式来引用 DIMENSIONS 分子句中的维度:
指定 WHERE 子句¶
以下示例指定 WHERE 子句来引用 DIMENSIONS 子句中的维度:
在 WHERE 子句中指定事实¶
The following example uses the region.r_name fact in a condition in the WHERE clause:
Specifying the name of the semantic view in the FROM clause¶
You can specify the name of the semantic view in the FROM clause of a SELECT statement, as you would when querying a standard SQL view:
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
- Selecting metrics
- Selecting dimensions
- Specifying the WHERE clause
- Specifying the HAVING clause
- Limitations with specifying the semantic view name in the FROM clause
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:
Selecting metrics¶
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:
Note
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:
You can also define and select ad-hoc metrics by passing a dimension or fact to any aggregate function. For example:
Selecting dimensions¶
If the SELECT list includes dimensions, you must specify those dimensions in the GROUP BY clause. For example:
In the SELECT list and in the GROUP BY clause, you can specify a dimension or a scalar expression that uses a dimension or a fact. For example:
指定 WHERE 子句¶
In the WHERE clause, you can only use conditional expressions that refer to dimensions or facts. For example:
The dimensions must be reachable by every metric used in the query.
Specifying the HAVING clause¶
In the HAVING clause, you can only specify metrics, and you must pass them to one of the aggregate functions listed in Selecting metrics. For example:
Limitations with specifying the semantic view name in the FROM clause¶
You cannot specify the following in the SELECT statement:
-
Extensions of the FROM clause, including:
- PIVOT
- UNPIVOT
- MATCH_RECOGNIZE
- LATERAL
-
Joins
-
Window function calls
-
QUALIFY
-
Subqueries
为给定指标选择可以返回的维度
当您指定要返回的维度和指标时,该维度的基表必须与该指标的基表相关。此外,维度的基表粒度必须等于或低于指标的基表。
For example, suppose that you query the tpch_analysis semantic view that you created in Example of using SQL to create a semantic view, and you want to return
the orders.order_date dimension and the customer.customer_order_count metric:
This query fails because the orders table for the order_date dimension has a higher level of granularity than the
customer table for the customer_order_count metric:
To list the dimensions that you can return with a specific metric, run the SHOW SEMANTIC DIMENSIONS FOR METRIC command. For example:
处理输出中重复的列名称
输出列使用指标和维度的非限定名称。如果您有多个具有相同名称的指标和维度,则多个列将使用相同的名称。
要解决此问题,请使用表别名为列分配不同的名称。
For example, suppose that you define the following semantic view, which defines the dimensions nation.name and
region.name:
If you query this view and select these two dimensions, the output includes two columns named name without any qualifiers:
To disambiguate the columns, use a table alias to assign different column names (for example, nation_name and
region_name):
定义和查询窗口函数指标
You can define metrics that call window functions and pass in aggregated values. These metrics are called window function metrics.
以下示例说明了窗口函数指标与将行级表达式传递给窗口函数的指标之间的区别:
-
以下指标是一个窗口函数指标:
In this example, the SUM window function takes another metric (
table_1.metric_3) as an argument.
以下指标也是一个窗口函数指标:
In this example, the SUM window function takes a valid metric expression (SUM(table_1.column_1)) as an argument.
-
The following metric is not a window function metric:
In this example, the SUM window function takes a column (
table_1.column_1) as an argument, and the result of that window function call is passed to a separate SUM aggregate function call.
以下部分说明如何定义和查询窗口函数指标:
定义窗口函数指标
When specifying a window function call, use this syntax, which is described in Parameters for window function metrics.
The following example creates a semantic view that includes the definitions of several window function metrics. The example uses tables from the TPC-DS sample database. For information on accessing this database, see Add the TPC-DS data set to your account.
您还可以在指标定义中使用同一逻辑表中的其他指标。例如:
Note
您不能在行级计算(数值和维度)或其他指标的定义中使用窗口函数指标。
查询窗口函数指标
When you query a semantic view and the query returns a window function metric, you must also return the dimensions specified in
PARTITION BY dimension, PARTITION BY EXCLUDING dimension, and ORDER BY dimension in the
CREATE SEMANTIC VIEW statement for the semantic view.
For example, suppose that you specify the date.date and date.year dimensions in the PARTITION BY EXCLUDING and ORDER BY
clauses in the definition of the store_sales.avg_7_days_sales_quantity metric:
If you return the store_sales.avg_7_days_sales_quantity metric in a query, you must also return the date.date and
date.year dimensions:
If you omit the date.date and date.year dimensions, an error occurs.
To determine which dimensions you must specify in the query, execute the
SHOW SEMANTIC DIMENSIONS FOR METRIC command. For example, to determine the dimensions that you must
specify when retrieving the store_sales.avg_7_days_sales_quantity metric, run this command:
In the output of the command, the required column contains true for the dimensions that you must specify in the query.
The following additional examples query the window function metrics defined in 定义窗口函数指标. Note that the DIMENSIONS clause includes the dimensions specified in the PARTITION BY EXCLUDING and ORDER BY clauses of the metric definitions.
以下示例会返回 30 天前的销售数量:
以下示例会返回 30 天前总销售数量的 7 天滚动平均值: