计算成员的聚合

时间:2014-12-26 18:54:47

标签: mdx olap olap-cube

我将代表我的任务的原型,以便于重现。这不是一个真实的环境。

  1. 我想明确计算我的产品没有销售,所以创建calc成员:

    WITH
    MEMBER [Measures].[Dead products count] AS
        SUM([Product].[Product].[Product].Members,
            IIF([Measures].[Sales Amount] > 0, NULL,1)
        )
        ,FORMAT_STRING = '#,0'
        ,SOLVE_ORDER = 10
    SELECT
        NON EMPTY {
        {[Date].[Calendar Year].[Calendar Year].AllMembers}
        } DIMENSION PROPERTIES PARENT_UNIQUE_NAME, CHILDREN_CARDINALITY ON COLUMNS,
        {
        {[Measures].[Dead products count]}
        } DIMENSION PROPERTIES PARENT_UNIQUE_NAME, CHILDREN_CARDINALITY ON ROWS
    FROM [Adventure Works]
    
  2. 很酷,现在我想知道我的测量值汇总了两年(即2005-2006):

    WITH
    MEMBER [Measures].[Dead products count] AS
        SUM([Product].[Product].[Product].Members,
            IIF([Measures].[Sales Amount] > 0, NULL,1)
        )
        ,FORMAT_STRING = '#,0'
        ,SOLVE_ORDER = 10
    MEMBER [Measures].[Dead products count 2005-2006] AS
        Aggregate({[Date].[Calendar Year].[All].&[2005],[Date].[Calendar Year].[All].&[2006]}
        ,
        [Measures].[Dead products count]
        )
        ,FORMAT_STRING = '#,0'
        ,SOLVE_ORDER = 10
    SELECT
        NON EMPTY {
        {[Date].[Calendar Year].[Calendar Year].AllMembers}
        } DIMENSION PROPERTIES PARENT_UNIQUE_NAME, CHILDREN_CARDINALITY ON COLUMNS,
        {
        {[Measures].[Dead products count],
        [Measures].[Dead products count 2005-2006]}
        } DIMENSION PROPERTIES PARENT_UNIQUE_NAME, CHILDREN_CARDINALITY ON ROWS
    FROM [Adventure Works]
    
  3. 不,发生错误是因为它无法汇总计算出的度量值。我找到另外两种解决方法:创建计算集(但对于大量成员来说效果太慢)和VisualTotals:

    WITH
    MEMBER [Measures].[Dead products count] AS
        SUM([Product].[Product].[Product].Members,
            IIF([Measures].[Sales Amount] > 0, NULL,1)
        )
        ,FORMAT_STRING = '#,0'
        ,SOLVE_ORDER = 10
    MEMBER [Measures].[Dead products count 2005-2006] AS
        (
            (VisualTotals
            ({[Date].[Calendar Year].[(All)] + [Date].[Calendar Year].[All].&[2005] + [Date].[Calendar Year].[All].&[2006]})
            ).Item(0)
        ,       
        [Measures].[Dead products count]
        )
        ,FORMAT_STRING = '#,0'
        ,SOLVE_ORDER = 10
    SELECT
        NON EMPTY {
        {[Date].[Calendar Year].[Calendar Year].AllMembers}
        } DIMENSION PROPERTIES PARENT_UNIQUE_NAME, CHILDREN_CARDINALITY ON COLUMNS,
        {
        {[Measures].[Dead products count],
        [Measures].[Dead products count 2005-2006]}
        } DIMENSION PROPERTIES PARENT_UNIQUE_NAME, CHILDREN_CARDINALITY ON ROWS
    FROM [Adventure Works]
    
  4. 是的,它有效!那么我的问题是什么?只需添加一个新的类似措施:

    WITH
    MEMBER [Measures].[Dead products count] AS
        SUM([Product].[Product].[Product].Members,
            IIF([Measures].[Sales Amount] > 0, NULL,1)
        )
        ,FORMAT_STRING = '#,0'
        ,SOLVE_ORDER = 10
    MEMBER [Measures].[Dead products count 2005-2006] AS
        (
            (VisualTotals
            ({[Date].[Calendar Year].[(All)] + [Date].[Calendar Year].[All].&[2005] + [Date].[Calendar Year].[All].&[2006]})
            ).Item(0)
        ,
        [Measures].[Dead products count]
        )
        ,FORMAT_STRING = '#,0'
        ,SOLVE_ORDER = 10
    MEMBER [Measures].[Dead products count 2007-2008] AS
        (
            (VisualTotals
            ({[Date].[Calendar Year].[(All)] + [Date].[Calendar Year].[All].&[2007] + [Date].[Calendar Year].[All].&[2008]})
            ).Item(0)
        ,
        [Measures].[Dead products count]
        )
        ,FORMAT_STRING = '#,0'
        ,SOLVE_ORDER = 10
    SELECT
        NON EMPTY {
        {[Date].[Calendar Year].[Calendar Year].AllMembers}
        } DIMENSION PROPERTIES PARENT_UNIQUE_NAME, CHILDREN_CARDINALITY ON COLUMNS,
        {
        {[Measures].[Dead products count],
        [Measures].[Dead products count 2005-2006],
        [Measures].[Dead products count 2007-2008]}
        } DIMENSION PROPERTIES PARENT_UNIQUE_NAME, CHILDREN_CARDINALITY ON ROWS
    FROM [Adventure Works]
    
  5. 这个错误或功能吗?我希望VisualTotals和Item(0)返回重新定义的所有成员,但它不适用于两个或更多计算的度量,但只有一个。

    P.S。请不要添加度量DistinctCount,因为事实表包含零和负值,并且我不想出于某些原因在存储级别上管理它。

    Update0: 在重新考虑该问题一段时间之后,我理解了以下内容:似乎VisualTotals()每次选择重新计算[All]成员一次,同时我重新计算它几次选择并返回最后重新计算的成员或错误消息(取决于大小写)。所以我有一个想法是在多维数据集中添加额外的不可见日期维度,以便重新计算其中的所有成员。这很棘手,但必须解决它。

1 个答案:

答案 0 :(得分:0)

实际使用后,VisualTotals()函数正在更改语句中的层次结构聚合规则。我不知道更改两次聚合规则意味着什么,并且我已阅读的文档中没有解释这种效果。

但要计算两年内的独特产品,您可以重新使用CurrentOrdinal()函数中提到的想法(注意item()是从0开始的,currentOrdinal()是1-为基础)。