MDX用于标准化度量集

时间:2013-10-08 19:13:37

标签: mdx

我正在尝试为此处记录的SSRS报告创建数据集:

http://sqlblog.com/blogs/stacia_misner/archive/2010/10/08/29249.aspx

挑战在于我有多个度量,我想要将哪些数据包含在度量列中,并且我希望在RowValue列中包含度量的名称。因此,以下查询仅返回度量“Sales Amount”的数据:

with
member [Measures].[Measure] as [Measures].[Sales Amount]
member [Measures].[RowValue] as [Product].[Category].CurrentMember.Name
member [Measures].[ColumnValue] as [Date].[Calendar Year].CurrentMember.Name
select {[Measures].[Measure], [Measures].[RowValue], [Measures].[ColumnValue]} on columns,
non empty ([Product].[Category].[Category].Members, [Date].[Calendar Year].[Calendar Year].Members) on rows
from [Adventure Works]

我想要做的是运行以下类型的查询,但是在上面的查询结构中返回的数据允许我将其插入到SSRS报告矩阵中:

WITH 
       MEMBER measures.SalesAmount AS [Measures].[Sales Amount]
       MEMBER measures.CustomerCount AS [Measures].[Customer Count]
       MEMBER measures.InternetFreightCost AS [Measures].[Internet Freight Cost]
SELECT [Date].[Calendar Year].[Calendar Year].Members ON COLUMNS, 
    {measures.SalesAmount,measures.CustomerCount,measures.InternetFreightCost} ON ROWS
FROM [Adventure Works]

任何MDX忍者都知道MDX是否可以实现这一点吗?

1 个答案:

答案 0 :(得分:0)

with member [Geography].[City].[Sales Amount] as 1
     member [Geography].[City].[Customer Count] as 1
     member [Geography].[City].[Freight Cost] as 1
     member [Measures].[RowValue] as [Geography].[City].CurrentMember.Name
     member [Measures].[ColumnValue] as [Date].[Calendar Year].CurrentMember.Name
     member [Measures].[Measure] as 
            CASE
                WHEN [Geography].[City].CurrentMember IS [Geography].[City].[Sales Amount]
                     THEN ([Measures].[Internet Sales Amount], [Geography].[City].[All Geographies])
                WHEN [Geography].[City].CurrentMember IS [Geography].[City].[Customer Count]
                     THEN ([Measures].[Customer Count], [Geography].[City].[All Geographies])
                WHEN [Geography].[City].CurrentMember IS [Geography].[City].[Freight Cost]
                     THEN ([Measures].[Internet Freight Cost], [Geography].[City].[All Geographies])
            END

select {[Measures].[RowValue], [Measures].[ColumnValue], [Measures].[Measure]}
       on columns,
       { [Geography].[City].[Sales Amount], [Geography].[City].[Customer Count], [Geography].[City].[Freight Cost]}
         * 
       [Date].[Calendar Year].[Calendar Year].Members
       having [Measures].[Measure] <> null
       on rows
from [Adventure Works]

应该提供你想要的东西。我使用[Geography].[City]作为实用程序层次结构。这可以是查询中未使用的任何层次结构。我选择了这个,因为它与查询中使用的两个度量组无关,因此不太可能在任何查询中使用。一些Cube设计师在其立方体中创建一个或两个单成员虚拟维度,这些维度与任何度量值组无关,并且可以像这里一样使用,以便在其上创建计算成员。

ReportingServices查询的一个难点是度量必须始终位于列中,并且列中不能包含其他层次结构。因此,如果我们想要在行中使用度量,我们必须将它们移动到另一个层次结构。这有两个步骤:首先,我们在实用程序层次结构上创建虚拟成员,然后将这些成员映射到CASE定义的[Measures].[Measure]构造中所需的度量,我们需要使用默认值实用维度的成员(在大多数情况下是All成员),以获得与我用于虚拟值的1不同的东西。

最后:non empty无法正常使用此构造,因为[Measures].[RowValue][Measures].[ColumnValue]永远不会为空。因此我将其替换为HAVING,它可以查看行中的特定列值。