如何在mdx中使用avg函数?

时间:2015-08-07 13:01:21

标签: sql mdx business-intelligence

我有mdx查询:

SELECT 
  NON EMPTY 
    {[Measures].[Amount]} ON COLUMNS
 ,NON EMPTY 
    {[Dim Date].[Day Of Week].[Day Of Week].MEMBERS} ON ROWS
FROM 
(
  SELECT 
    [Dim Date].[Date Int].&[20140730] : [Dim Date].[Date Int].&[20150730] ON COLUMNS
  FROM [Cube]
  WHERE 
    [Dim Client].[Common Client UID].&[{some id}]
);

所以我有一个工作日昏暗,其中包含成员作为1-7的数字。查询查找每个工作日的返回金额,总结,但我想找出一个平均值,所以不知怎的,我需要找出总计多少项给我[测量]。[金额]结果。我尝试过没有工作的单独成员函数。

WITH MEMBER [Measures].[Avg] AS
    Avg(  
      ( [Dim Date].[Day Of Week].CURRENTMEMBER, [Measures].[Amount] )
   )

平均返回相同的值。我如何在mdx中执行此类请求?

1 个答案:

答案 0 :(得分:2)

这将为您提供 over 1成员:

WITH MEMBER [Measures].[Avg] AS
    Avg(  
      ( [Dim Date].[Day Of Week].CURRENTMEMBER, [Measures].[Amount] )
   )

这是因为CURRENTMEMBER正在返回1个成员。

如果您希望平均数超过几个成员,则需要提供 set 作为Avg函数的第一个参数。这是一个例子:

WITH MEMBER [Measures].[Daily Avg] AS
    Avg(  
      Descedants(
         [Date].[Date - Calendar Month].CURRENTMEMBER
        ,[Date].[Date - Calendar Month].[Calendar Day]
    ,[Measures].[Amount] 
   )

虽然我怀疑下面的内容应该适用于您的上下文:

WITH 
  MEMBER [Measures].[Avg] AS 
    Avg
    (
      (EXISTING 
        [Dim Date].[Date Int].MEMBERS)
     ,[Measures].[Amount]
    ) 
SELECT 
  NON EMPTY 
    {
      [Measures].[Amount]
     ,[Measures].[Avg]
    } ON COLUMNS
 ,NON EMPTY 
    {[Dim Date].[Day Of Week].[Day Of Week].MEMBERS} ON ROWS
FROM 
(
  SELECT 
    [Dim Date].[Date Int].&[20140730] : [Dim Date].[Date Int].&[20150730] ON COLUMNS
  FROM [Cube]
  WHERE 
    [Dim Client].[Common Client UID].&[{some id}]
);