如何重构MDX以在新行标签下对计算的度量进行分组

时间:2013-09-27 18:59:26

标签: sql mdx

我在以下MDX中查询日期范围(月)内两个指标的一组计算度量:

WITH
MEMBER [Measures].[Prior Visits Office New] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},[Measures].[Visits Office New]),format_string = '#,##0' 
MEMBER [Measures].[Current Visits Office New] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},[Measures].[Visits Office New]),format_string = '#,##0' 
MEMBER [Measures].[Diff Visits Office New] as ([Measures].[Current Visits Office New] - [Measures].[Prior Visits Office New]),format_string = '#,##0' 
MEMBER [Measures].[Percent Change Visits Office New] as ([Measures].[Diff Visits Office New] / [Measures].[Prior Visits Office New]),format_string = 'Percent'
MEMBER [Measures].[Prior Visits Office Established] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},[Measures].[Visits Office Established]),format_string = '#,##0' 
MEMBER [Measures].[Current Visits Office Established] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},[Measures].[Visits Office Established]),format_string = '#,##0' 
MEMBER [Measures].[Diff Visits Office Established] as ([Measures].[Current Visits Office Established] - [Measures].[Prior Visits Office Established]),format_string = '#,##0' 
MEMBER [Measures].[Percent Change Visits Office Established] as ([Measures].[Diff Visits Office Established] / [Measures].[Prior Visits Office Established]),format_string = 'Percent'
SELECT {[Measures].[Current Visits Office New],[Measures].[Prior Visits Office New],[Measures].[Diff Visits Office New],[Measures].[Percent Change Visits Office New],[Measures].[Current Visits Office Established],[Measures].[Prior Visits Office Established],[Measures].[Diff Visits Office Established],[Measures].[Percent Change Visits Office Established]}
ON COLUMNS , NON EMPTY Hierarchize(AddCalculatedMembers({DrilldownLevel({[Date Post Transaction].[Calendar Month Period].Children})}))  
ON ROWS FROM (SELECT ({[Date Post Transaction].[Calendar Month Period].&[201301]}) 
ON COLUMNS FROM [cube])
WHERE ([Report Group].[Report Group1].&[Group])

结果集如下所示:

+-----------------------------+-----------------------------+---------------------------+--------------------------+------------------------------------+-------------------------------------+-----------------------------------+---------------------------------+--------------------------------------------+
|                             |  Current Visits Office New  |  Prior Visits Office New  |  Diff Visits Office New  |  Percent Change Visits Office New  |  Current Visits Office Established  |  Prior Visits Office Established  |  Diff Visits Office Established |  Percent Change Visits Office Established  |
+-----------------------------+-----------------------------+---------------------------+--------------------------+------------------------------------+-------------------------------------+-----------------------------------+---------------------------------+--------------------------------------------+
|            201301           |            4,793            |           4,307           |             486          |               11.28%               |                 58,979              |                57,228             |               1,751             |                    3.06%                   |
+-----------------------------+-----------------------------+---------------------------+--------------------------+------------------------------------+-------------------------------------+-----------------------------------+---------------------------------+--------------------------------------------+

我只有[Date Post Transaction].[Calendar Month Period].&[201301],其中显示行201301作为填充符。它似乎没有返回没有行轴标签的结果集。

我需要获取结果集的格式如下所示:

+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|                                 |        Current       |        Prior        |          Diff         |    Percent Change    |  
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|    Visits Office New            |         4,793        |        4,307        |           486         |        11.28%        |
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|    Visits Office Established    |        58,979        |        57,228       |          1,751        |        3.06%         |
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+

甚至更好:

+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|    Visits Office New            |         4,793        |        4,307        |           486         |        11.28%        |
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|    Visits Office Established    |        58,979        |        57,228       |          1,751        |        3.06%         |
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+

所以,我正在思考一些逻辑,它可以将计算出的度量分组,如下所示:

WITH
MEMBER [Measures].[Visits Office New] as (
    MEMBER [Measures].[Prior Visits Office New] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},[Measures].[Visits Office New]),format_string = '#,##0' 
    MEMBER [Measures].[Current Visits Office New] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},[Measures].[Visits Office New]),format_string = '#,##0' 
    MEMBER [Measures].[Diff Visits Office New] as ([Measures].[Current Visits Office New] - [Measures].[Prior Visits Office New]),format_string = '#,##0' 
    MEMBER [Measures].[Percent Change Visits Office New] as ([Measures].[Diff Visits Office New] / [Measures].[Prior Visits Office New]),format_string = 'Percent' 
)
MEMBER [Measures].[Visits Office Established] as (
    MEMBER [Measures].[Prior Visits Office Established] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},[Measures].[Visits Office Established]),format_string = '#,##0'
    MEMBER [Measures].[Current Visits Office Established] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},[Measures].[Visits Office Established]),format_string = '#,##0' 
    MEMBER [Measures].[Diff Visits Office Established] as ([Measures].[Current Visits Office Established] - [Measures].[Prior Visits Office Established]),format_string = '#,##0' 
    MEMBER [Measures].[Percent Change Visits Office Established] as ([Measures].[Diff Visits Office Established] / [Measures].[Prior Visits Office Established]),format_string = 'Percent'
)
SELECT {[Measures].[Current Visits Office New],[Measures].[Prior Visits Office New],[Measures].[Diff Visits Office New],[Measures].[Percent Change Visits Office New],[Measures].[Current Visits Office Established],[Measures].[Prior Visits Office Established],[Measures].[Diff Visits Office Established],[Measures].[Percent Change Visits Office Established]}
ON COLUMNS , NON EMPTY Hierarchize(AddCalculatedMembers({DrilldownLevel({[Date Post Transaction].[Calendar Month Period].Children})}))  
ON ROWS FROM (SELECT ({[Date Post Transaction].[Calendar Month Period].&[201301]}) 
ON COLUMNS FROM [cube])
WHERE ([Report Group].[Report Group1].&[Group])

但这不起作用。想法?

1 个答案:

答案 0 :(得分:0)

使用目前未在查询中使用的任何层次结构(我将在我的示例中使用[Dim1].[Util],因为我不知道您的多维数据集),您将在此创建成员 - 因为您可以在任何上创建计算成员层次结构,而不仅仅是Measures层次结构:

WITH member [Dim1].[Util].[Prior] as
     SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},
         [Measures].CurrentMember
        ),format_string = '#,##0'
     member [Dim1].[Util].[Current] as
     SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},
         [Measures].CurrentMember
        ),format_string = '#,##0'
     member [Dim1].[Util].[Diff] as
     [Dim1].[Util].[Current] - member [Dim1].[Util].[Prior],format_string = '#,##0'
     member [Dim1].[Util].[Percent Change] as
     [Dim1].[Util].[Diff] / [Dim1].[Util].[Current],format_string = 'Percent'
SELECT {
       [Dim1].[Util].[Prior],
       [Dim1].[Util].[Current],
       [Dim1].[Util].[Diff],
       [Dim1].[Util].[Percent Change]
       }
ON COLUMNS ,
       {
       [Measures].[Visits Office New],
       [Measures].[Visits Office Established]
       }
ON ROWS
  FROM (SELECT ({[Date Post Transaction].[Calendar Month Period].&[201301]}) 
               ON COLUMNS
          FROM [cube])
WHERE ([Report Group].[Report Group1].&[Group])

对于要在列中使用的层次结构没有特定要求,但不应在查询中使用它。一些立方体设计者在每个立方体中明确创建一个或甚至两个实用程序维度,它们只包含一个虚拟成员,以便能够轻松编写这样的查询。然后,这些维度仅用于在其上创建计算成员。