【问题标题】:What does MDX Aggregate() do with a single argument?MDX Aggregate() 对单个参数有什么作用?
【发布时间】:2015-11-12 14:39:32
【问题描述】:

我了解如何使用 MDX Aggregate() 和 Sum() 函数,以及它们之间的区别。

(一个有趣的问题是,在该级别的子级的层次结构中的更高级别定义的度量的总和将该度量乘以子级的数量 - 而聚合“正确”返回仅在更高级别定义的值) .

MSDN 上记录的语法是:

Aggregate(Set_Expression [ ,Numeric_Expression ])

我一直将它与两个参数一起使用。但是,当只提供 set_expression 参数时,Aggregate 会做什么?文档(同样来自 MSDN)非常晦涩:

如果未提供数值表达式,则此函数使用为每个度量指定的默认聚合运算符聚合当前查询上下文中的每个度量。

我在这样的 MDX 查询中尝试过:

WITH MEMBER WeekSummedTotal AS
Aggregate([Days].[WeeksAndDays].CurrentMember.Children)
SELECT 
{Measures.ThingoCount,Measures.WeekTotal,Measures.WeekSummedTotal} ON 0,
[Days].[WeeksAndDays].[WeekName] ON 1
FROM DateGRoupingTest

这会做什么? Aggregate 会在集合上聚合多维数据集的默认度量吗?还是设置Measures.Members?还是在 0 轴上指定的其他度量值集?

这些都不是!查询运行并返回结果,但 calcd 度量 WeekSumTotal 显示 #Error,出现完全无意义的错误:

聚合函数不能用于度量维度中的计算成员

现在这是真的,但完全无关紧要。查询中的其他度量均未计算,实际上多维数据集没有任何计算成员。那么 Aggregate() 究竟想在这里做什么呢?这个错误消息(同样,在 MDX 中!)是否完全具有误导性?

补充:@whytheq 在下面的答案中建议使用聚合创建计算度量,但在备用维度层次结构上而不是在度量维度中创建它。这有效,但前提是包含与所选“任何旧...”维度的 [All] 成员的交叉连接。 在那里创建度量也使得无法将两个(基本)度量和计算的度量放在同一轴上。如果我尝试这样做:

{Measures.ThingoCount,Measures.WeekTotal,[Ages].[Age Key].WeekSummedTotal} ON 0,

我收到非常无用的错误消息:

Members, tuples or sets must use the same hierarchies in the  function.

我认为,这意味着“我无法在Measures 成员和[Ages].[Age Key] 成员之间使用, (UNION) 函数创建集合,因为它们是不同维度的成员”。

我的结论是,由于您提供的信息丰富的答案,带有单个参数的 Aggregate() 是一个棘手的野兽;我想知道为什么它的第二个参数是可选的?

我还注意到,尝试在我的年龄维度上创建我的计算成员(只有一个层次结构,只有一个属性)会给我带来误导性的错误消息:

The 'Ages' dimension contains more than one hierarchy, therefore
the hierarchy must be explicitly specified.

除非我明确指定层次结构。 MDX 有很大的潜力,但如果 MS 付出更多努力让它正确地反馈错误,学习曲线会变得更加平缓。

【问题讨论】:

  • 你为什么不能把它写成Sum([Days].[WeeksAndDays].CurrentMember.Children, [Measures].SomeMeasure)?问题解决了。
  • 当然,这要容易得多,并且确实给了我想要的结果(尽管就我的目的而言,Aggregate 比 Sum 更好)。我只是想更好地理解 MDX:我想知道 Sum/Aggregate 的这种“没有第二个参数”用法实际上做了什么,进行了实验,发现结果令人困惑。但我认为你和@whytheq 已经解释了正在发生的事情。
  • @SebTHU 这是一个非常基本的mdx 错误Members, tuples or sets must use the same hierarchies in the function.:如果您使用花括号{x,y,z} 创建一个集合,那么x、y 和z 必须是相同的dimensionality - 或者你得到那个错误。一个类似的基本错误与该错误相反 - 如果您使用普通大括号 (x,y,z) 创建一个元组,则 x、y 和 z 必须来自不同的层次结构。
  • @SebTHU I wonder why it was designed with the second argument optional? - 更改查询很容易,因此您可以看到聚合何时非常有用 - 我将在我的答案中添加一个编辑。
  • 这是一个参数的用例示例。但请注意,以方式使用它会破坏维度层次结构,通常当我这样做时,我会交叉加入层次结构中的成员:stackoverflow.com/q/17682164/84206

标签: ssas mdx


【解决方案1】:

获取 MSDN 定义的这一部分:

...此函数聚合当前查询中的每个度量 上下文...

每个在脚本上下文中的度量如下:

{Measures.ThingoCount,Measures.WeekTotal,Measures.WeekSummedTotal}

现在Measures.WeekSummedTotal 是度量维度中的计算成员 - 因此出现错误。

我想像下面这样可以正常工作,您使用Aggregate 在Measures 以外的维度中创建成员?:

WITH 
  MEMBER [SomeSpareDim].[SomeSpareHier].WeekSummedTotal AS 
    Aggregate
    (
      [Days].[WeeksAndDays].CurrentMember.Children * [SomeDim].[SomeHier].[All]
    ) 
SELECT 
  [SomeSpareDim].[SomeSpareHier].WeekSummedTotal ON 0
 ,[Days].[WeeksAndDays].[WeekName] ON 1
FROM DateGRoupingTest;

上面的内容可以更改以显示 Aggregate 非常有用:

WITH 
  MEMBER [Days].[WeeksAndDays].[Last3Weeks] AS 
    Aggregate
    (
      {
       [Days].[WeeksAndDays].[Weekx]
      ,[Days].[WeeksAndDays].[Weeky]
      ,[Days].[WeeksAndDays].[Weekz]
      }
    ) 
SELECT 
  {Measures.ThingoCount,Measures.WeekTotal} ON 0
 ,{
     //<< the following custom aggregated member will work for any measure, that is ON 0, via Aggregate
     //it can be mixed up with the normal members of the same hierarchy like the following
    [Days].[WeeksAndDays].[Last3Weeks]  
   ,[Days].[WeeksAndDays].[WeekName].members
  } ON 1
FROM DateGRoupingTest;

【讨论】:

  • @GregGalloway - 给你一个!
  • 我认为计算的度量试图引用自身是对的。您的代码运行没有错误,计算的成员聚合了Measures.DefaultMember 度量。在我对问题的编辑中提供更多反馈。元问题:我可以将您的答案和@SouravA 的答案都标记为答案吗?它们都是非常有用的答案。
  • @SebTHU 随便你。我很高兴发现您的度量是引用自身。我之前可能已经看到过这个异常,并且没有从逻辑上思考为什么 - 谢谢你的问题。
【解决方案2】:

这会做什么?将 Aggregate 聚合多维数据集的默认值 测量集合?还是设置Measures.Members?或者其他的集合 在 0 轴上指定的度量值?

Aggregate 函数在Measures 维度的当前 度量上聚合集合。如果一项措施在范围内,则该措施是“当前的”。如果度量不在范围内,则考虑使用来自 measures 维度的默认成员进行聚合。

可以通过多种方式将度量添加到范围,例如

在坐标轴上测量

with member [Customer].[Customer].abc as
aggregate([Customer].[Customer].members)


select [Customer].[Customer].abc on 0,
{[Measures].[Internet Sales Amount],[Measures].[Reseller Sales Amount]}  on 1
from [Adventure Works]

在上面的示例中,成员 abc 被计算了两次,每次测量一次。

使用子立方体

with member [Customer].[Customer].abc as
aggregate([Customer].[Customer].members)


select [Customer].[Customer].abc on 0
from (select {[Measures].[Internet Sales Amount] } on 0 from [Adventure Works])

定义中的度量

with member [Customer].[Customer].abc as
aggregate([Customer].[Customer].members, [Measures].[Internet Sales Amount])


select [Customer].[Customer].abc on 0
from [Adventure Works]

在Where 子句中

with member [Customer].[Customer].abc as
aggregate([Customer].[Customer].members)


select [Customer].[Customer].abc on 0
from [Adventure Works]
where [Measures].[Internet Sales Amount]

正如Whytheq 所建议的那样,将成员放在其他维度层次结构组合中。否则,聚合函数可能会导致计算成员自引用。

【讨论】:

  • 对“当前查询上下文中的每个度量”实际上相当于什么的非常透彻的解释!为什么他们不付钱给你写 MSDN 文档? ;)
  • 谢谢,受宠若惊。我希望在接下来的几十年里写一篇。
猜你喜欢
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-24
相关资源
最近更新 更多