您的位置:首页 > 运维架构

基于Hadoop生态圈的数据仓库实践 —— 进阶技术(六)

2016-07-25 13:51 696 查看
六、维度层次
大多数维度都具有一个或多个层次。例如,日期维度就有一个四级层次:年、季度、月和日。这些级别用date_dim表里的列来表示。日期维度是一个单路径层次,因为除了年-季度-月-日这条路径外,它没有任何其它层次。本节讨论在维度的层次上进行分组和钻取查询。多路径层次在下一节“多路径和参差不齐的层次”中讨论。
为了识别数据仓库里一个维度的层次,首先要理解维度中列的含义,然后识别两个或多个列是否具有相同的主题。例如,日、月、季度和年具有相同的主题因为它们都是关于日期的。具有相同主题的列形成一个组,组中的一列必须包含至少一个组内的其它成员,例如,在前面提到的组中,月包含日。这些列的链条形成了一个层次。例如,日-月-季度-年这个链条是一个日期维度的层次。除了日期维度,产品和客户维度也有层次。
下表显示了三个维度的层次。注意客户维度具有两个路径的层次。
customer_dimptoduct_dimdate_dim
customer_street_addressshipping_addressproduct_namedate
customer_zip_codeshipping_zip_codeproduct_categorymonth_name
customer_cityshipping_cityquarter
customer_stateshipping_stateyear
分组和钻取查询
可以在层次上进行分组和钻取查询。分组查询是把度量按照一个维度的一个或多个级别进行分组。下面的脚本是一个分组查询的例子。这个查询按产品(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;
查询结果如下图所示。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: