MDX:总结,忽略名称重复

时间:2016-06-09 15:29:12

标签: mdx iccube iccube-reporting

我正在尝试将某个类别及其子类别中所有产品的销售额相加,只为具有相同产品维度密钥的产品添加销售额一次。

每个类别可以包含任意数量的子类别,每个子类别可以拥有自己的任意数量的子类别,最多五个子类别。子类别的数量可以变化 因此category_hierarchy中的category_dimension具有以下级别:

main_categorysub_category_1sub_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-06-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

1 个答案:

答案 0 :(得分:0)

这还没有完成或测试 - 这只是最初的想法:一旦我有权访问它,我就需要对AdvWrks进行测试。

根据您的需要调整所有成员是非常复杂的。

WITH 
SET [All_tuples] AS
    ORDER(
      {[CATEGORY_DIMENSION].[CATEGORY_HIERARCHY].[CATEGORY_HIERARCHY].MEMBERS}
    * {[PRODUCT_DIMENSION].[PRODUCT_HIERARCHY].MEMBERS}
    , [PRODUCT_DIMENSION].CURRENTMEMBER.MEMBER_CAPTION
    , BDESC
    )
SET [NoDupes] AS
  FILTER(
     [All_tuples] AS X,
     X.CURRENT.ITEM(1) NOT IS X.CURRENT.LAG(1).ITEM(1)
  )
MEMBER [CATEGORY_DIMENSION].[CATEGORY_HIERARCHY].[All].[Category I NEW] AS
  AGGREGATE(
    [NoDupes]
  ) 
SELECT 
  {[Measures].[Sales]} ON COLUMNS
 ,NON EMPTY 
   [CATEGORY_DIMENSION].[CATEGORY_HIERARCHY].[All].[Category I NEW] ON ROWS
FROM [Cube];