MDX运行总计与交叉连接

时间:2015-06-05 11:51:56

标签: mdx

MDX新手问题

我正在使用FoodMart示例数据库来表达我的问题。

我需要显示" Unit Sales"在列上测量 使用"月","年"和"产品名称"行上的尺寸:

Year    Month     Product   |  Unit Sales    UnitSoldIncludngThisProdTillDate
                            |
2014    Jan        P1       |    4                4
                   P2       |    2                6
                   P3       |    0                6
        Feb        P1       |    1                7
                   P2       |    0                7
                   P3       |    3               10
2015    Jan        P1       |    7               17

......等等

如果没有与Product的交叉连接,查询运行正常。 但是,加入Product并不能满足我的需求。

如何解决这个问题?

我正在运行的MDX查询是

AGGREGATE(YTD(), [Measures].[Unit Sales])
SELECT {[Measures].[Unit Sales], MEASURES.YTDDEMO} ON 0,
NON EMPTY {[Time].[Month].Members * [Product].[ProductLevel].Members} ON 1
FROM  [TestFoodMart]

于2015年6月10日添加

我一直在使用正确的语法等。

没有加入我得到以下内容:

|                  | Unit Sales | Sales To Date |
+------+-----------+------------+---------------+
| 2013 | April     |     45,049 |        45,049 |
|      | August    |     44,777 |        89,826 |
|      | December  |            |        89,826 |
|      | February  |     44,431 |       134,257 |
|      | January   |     46,313 |       180,570 |
|      | July      |     46,671 |       227,241 |
|      | June      |     45,611 |       272,852 |
|      | March     |     46,334 |       319,186 |
|      | May       |     45,085 |       364,271 |
|      | November  |     53,807 |       418,078 |
|      | October   |     43,945 |       462,023 |
|      | September |     47,964 |       509,987 |

通过加入我得到以下内容:

                                                                | Unit Sales | YTDDEMO |
+------+-----------+---------------------------------------------+------------+---------+
| 2013 | April     | ADJ Rosy Sunglasses                         |         38 |      38 |
|      |           | Akron City Map                              |         29 |      29 |
|      |           | Akron Eyeglass Screwdriver                  |         34 |      34 |
|      |           | American Beef Bologna                       |         28 |      28 |
|      |           | American Chicken Hot Dogs                   |         25 |      25 |
|

如您所见,聚合无法正常工作

2 个答案:

答案 0 :(得分:0)

您错过了WITH代码行:

WITH MEMBER MEASURES.YTDDEMO AS
  AGGREGATE(YTD(), [Measures].[Unit Sales])
SELECT 
  {[Measures].[Unit Sales], MEASURES.YTDDEMO} ON 0,
  NON EMPTY 
  {[Time].[Month].Members * [Product].[ProductLevel].Members} ON 1
FROM  [TestFoodMart]

请注意MSDN功能的YTD定义中的备注部分:https://msdn.microsoft.com/en-us/library/ms146039.aspx

  

如果未指定成员表达式,则默认为当前   第一个层次结构的成员,第一个层次结构的类型为Year   度量组中类型的维度。 Ytd函数是一个   PeriodsToDate函数的快捷函数所在的Type   设置了级别所基于的属性层次结构的属性   多年。也就是说,Ytd(Member_Expression)相当于   PeriodsToDate(Year_Level_Expression,Member_Expression)。请注意这一点   当Type属性设置为FiscalYears时,函数将不起作用。

如果年份属性层次结构的type属性未设置为time,则YTD函数将不起作用。

这个等效版本有用吗?

WITH 
  MEMBER MEASURES.YTDDEMO AS 
    Aggregate
    (
      PeriodsToDate([Time].[Year]) <<//change to what your year level is
     ,[Measures].[Unit Sales]
    ) 
SELECT 
  NON EMPTY 
    {
      [Measures].[Unit Sales]
     ,MEASURES.YTDDEMO
    } ON 0
 ,NON EMPTY 
    {[Time].[Month].Members * [Product].[ProductLevel].Members} ON 1
FROM [Adventure Works];

答案 1 :(得分:-1)

你能尝试一下,我认为这只是一个语法问题。

AGGREGATE(YTD(), [Measures].[Unit Sales])
SELECT {[Measures].[Unit Sales], MEASURES.YTDDEMO} ON 0,
NON EMPTY ([Time].[Month].Members * [Product].[ProductLevel].Members) ON 1
FROM  [TestFoodMart]
相关问题