MDX如何在几个月内返回平均值

时间:2016-04-20 03:25:22

标签: mdx

我一直在关注AVG(MDX),希望能够在几个月内获得平均值。

将其用作simpla MDX:

    SELECT NON EMPTY 
    {[Measures].[Freight]} ON COLUMNS
 ,NON EMPTY 
    {[Due Date].[Calendar Month].[Calendar Month].MEMBERS} ON ROWS
FROM [Adventure Works Cube];

它将返回:

enter image description here

是否可以返回:

    Freight
August  108567.2463
February    208635.5203
July    103180.7446
March   209712.7297
November    103873.3682
Avg Freight 146793.9218

我试过了:

WITH MEMBER [Measures].[Average Freight] AS
    AVG([Due Date].[Calendar Month].[All], 
        [Measure].[Sale])
SELECT NON EMPTY 
    {[Measures].[Freight],
    [Measures].[Average Freight]} ON COLUMNS
 ,NON EMPTY 
    {[Due Date].[Calendar Month].[Calendar Month].MEMBERS} ON ROWS
FROM [Adventure Works Cube];

它只会返回:

enter image description here

2 个答案:

答案 0 :(得分:1)

如果你这样做,你就得不到平均值:

WITH 
  MEMBER [Measures].[Average Freight] AS 
    Avg([Measures].[Freight Cost]) 
SELECT 
  {
    [Measures].[Average Freight]
   ,[Measures].[Freight Cost]
  } ON COLUMNS
 ,NON EMPTY 
    [Date].[Calendar].[Month].MEMBERS ON ROWS
FROM [Adventure Works];

平均值与实际测量值相同:

enter image description here

要做一个平均值,你需要一个SET来做平均值:

WITH 
  MEMBER [Measures].[Average Freight] AS 
    Avg
    (
      [Date].[Calendar].CurrentMember.Children
     ,[Measures].[Freight Cost]
    ) 
SELECT 
  {
    [Measures].[Average Freight]
   ,[Measures].[Freight Cost]
  } ON COLUMNS
 ,NON EMPTY 
    [Date].[Calendar].[Month].MEMBERS ON ROWS
FROM [Adventure Works];

所以现在平均衡量标准是有意义的:

enter image description here

答案 1 :(得分:0)

在Adventure Works的测试示例下面。 msdn Avg (MDX) link

 WITH  MEMBER [Measures].[Average Freight] AS
 AVG ([Measures].[Freight Cost]
)
SELECT
   [Measures].[Average Freight] ON COLUMNS,
  NON EMPTY   [Date].[Calendar].[Month].Members ON ROWS
FROM
   [Adventure Works];

结果

          Average Freight
July 2005       24067.9287
August 2005     51115.0148999999
September 2005  40996.0152
October 2005    33951.2746
....