大多数维度都具有一个或多个层次。例如,日期维度就有一个四级层次:年、季度、月和日。这些级别用date_dim表里的列来表示。日期维度是一个单路径层次,因为除了年-季度-月-日这条路径外,它没有任何其它层次。本节讨论在维度的层次上进行分组和钻取查询。多路径层次在下一节“多路径和参差不齐的层次”中讨论。
为了识别数据仓库里一个维度的层次,首先要理解维度中列的含义,然后识别两个或多个列是否具有相同的主题。例如,日、月、季度和年具有相同的主题因为它们都是关于日期的。具有相同主题的列形成一个组,组中的一列必须包含至少一个组内的其它成员,例如,在前面提到的组中,月包含日。这些列的链条形成了一个层次。例如,日-月-季度-年这个链条是一个日期维度的层次。除了日期维度,产品和客户维度也有层次。
下表显示了三个维度的层次。注意客户维度具有两个路径的层次。
customer_dim |
ptoduct_dim |
date_dim |
|
customer_street_address |
shipping_address |
product_name |
date |
customer_zip_code |
shipping_zip_code |
product_category |
month_name |
customer_city |
shipping_city |
|
quarter |
customer_state |
shipping_state |
|
year |
分组和钻取查询
可以在层次上进行分组和钻取查询。分组查询是把度量按照一个维度的一个或多个级别进行分组。下面的脚本是一个分组查询的例子。这个查询按产品(product_category列)和日期维度的三个层次级别(year、quarter和month列)分组返回销售金额。
USE dw;查询结果如下图所示。分组查询的输出显示了每一行的度量(销售订单金额)都沿着年-季度-月的层次分组。
SELECT product_category,
year,
quarter,
month,
SUM(order_amount) sum_order_amount
FROM sales_order_fact a,
product_dim b,
date_dim c
WHERE a.product_sk = b.product_sk
AND a.order_date_sk = c.date_sk
GROUP BY product_category , year , quarter , month
CLUSTER BY product_category , year , quarter , month;
与分组查询类似,钻取查询也把度量按照一个维度的一个或多个级别进行分组。但与分组查询不同的是,分组查询只显示分组后最低级别(本例中是月级别)上的度量(订单金额的汇总),而钻取查询显示分组后维度每一个级别的度量。下面使用两种方法进行钻取查询,结果显示了每个日期维度级别(年、季度和月级别)的订单汇总金额。
USE dw; -- 使用union all SELECT product_category, time, order_amount FROM(SELECT product_category, case when sequence = 1 then concat('year: ', time) when sequence = 2 then concat('quarter: ', time) else concat('month: ', time) end time, order_amount, sequence, date FROM (SELECT product_category, min(date) date, year time, 1 sequence, SUM(order_amount) order_amount FROM sales_order_fact a, product_dim b, date_dim c WHERE a.product_sk = b.product_sk AND a.order_date_sk = c.date_sk GROUP BY product_category , year UNION ALL SELECT product_category, min(date) date, quarter time, 2 sequence, SUM(order_amount) order_amount FROM sales_order_fact a, product_dim b, date_dim c WHERE a.product_sk = b.product_sk AND a.order_date_sk = c.date_sk GROUP BY product_category , year , quarter UNION ALL SELECT product_category, min(date) date, month time, 3 sequence, SUM(order_amount) order_amount FROM sales_order_fact a, product_dim b, date_dim c WHERE a.product_sk = b.product_sk AND a.order_date_sk = c.date_sk GROUP BY product_category , year , quarter , month) x CLUSTER BY product_category , date , sequence , time) y; -- 使用grouping__id函数SELECT product_category, time, order_amount FROM(SELECT product_category, case when gid = 3 then concat('year: ', year) when gid = 7 then concat('quarter: ', quarter) else concat('month: ', month) end time, order_amount, gid, date FROM(SELECT product_category, year, quarter, month, min(date) date, SUM(order_amount) order_amount, CAST(grouping__id AS INT) gid FROM sales_order_fact a, product_dim b, date_dim c WHERE a.product_sk = b.product_sk AND a.order_date_sk = c.date_sk GROUP BY product_category , year , quarter , month with rollup) x WHERE gid > 1 CLUSTER BY product_category , date , gid , time) y;
查询结果如下图所示。