我正在尝试将一个类别及其子类别中所有产品的销售额相加,仅将具有相同产品维度键的产品的销售额相加一次。
每个类别可以有任意数量的子类别,每个子类别可以有自己的任意数量的子类别,最多有五个级别的子类别。子类别的数量可能会有所不同。
所以category_hierarchy
incategory_dimension
有这些级别:
main_category
, sub_category_1
, sub_category_2
, ...,sub_category_5
看起来像这样:
id main_category sub_category_1 sub_category_2 sub_category_3 ...
1 Electronics
2 Electronics Camera & Photo
3 Electronics Camera & Photo Accessories
4 Electronics Camera & Photo Digital Cameras
5 Electronics Camera & Photo Film Photography Film Cameras
6 Electronics Camera & Photo Film Photography Film
7 Electronics Headphones
8 Home & Kitchen
...
在事实表中,每天每个类别的每个产品都有销售数字,并且每个产品可以属于多个类别(如果产品属于多个类别,则每个类别的销售数字相同),例如
id product_id category_id date_id sales other_information
1 1 1 2016-06-30 4 AAA
2 1 2 2016-06-30 4 BBB
3 1 6 2016-06-30 4 CCC
4 2 2 2016-06-30 5 DDD
5 2 4 2016-06-30 5 EEE
...
如果我现在运行一个简单的 MDX 查询来向我显示每个类别的销售数量,如果产品属于多个类别,它将多次汇总销售数量:
SELECT
{[DATE_DIMENSION].[DATE_HIERARCHY].ALLMEMBERS} ON COLUMNS
,NON EMPTY
{[CATEGORY_DIMENSION].[CATEGORY_HIERARCHY].ALLMEMBERS}
*
{[PRODUCT_DIMENSION].[PRODUCT_HIERARCHY].ALLMEMBERS} ON ROWS
FROM [Cube]
WHERE [MEASURES].[SALES];
返回
2016-06-30 2016-06-29 ...
CATEGORY PRODUCT_ID SALES SALES
All Categories All Products 22 ...
1 12
2 10
Electronics All Products 22
1 12
2 10
Camera & Photo All Products 18
1 8
2 10
Digital Cameras All Products 5
2 5
Film Photography All Products 4
1 4
Film All Products 4
1 4
我希望始终在 2016 年 6 月 30 日获得产品 1 的销售额 4 和产品 2 的销售额 5(但仍然能够按周/月/年汇总销售额):
2016-06-30 2016-06-29 ...
CATEGORY PRODUCT_ID SALES SALES
All Categories All Products 9 ...
1 4
2 5
Electronics All Products 9
1 4
2 5
Camera & Photo All Products 9
1 4
2 5
Digital Cameras All Products 5
2 5
Film Photography All Products 4
1 4
Film All Products 4
1 4