分享方式:


Avg (MDX)

評估集合,並傳回集合中儲存格的非空白值平均值、在集合中的量值或指定量值上平均。

語法

  
Avg( Set_Expression [ , Numeric_Expression ] )  

引數

Set_Expression
傳回集合的有效多維度運算式 (MDX) 運算式

Numeric_Expression
有效的數值運算式,通常是傳回數位之儲存格座標的多維度運算式 (MDX) 運算式。

備註

如果指定了一組空白元組或空集合, Avg 函式會傳回空值。

Avg 式會先計算指定集合中儲存格的空值平均值,方法是先計算指定集合中各儲存格的值總和,然後將計算總和除以指定集合中無空白儲存格的計數。

注意

Analysis Services 在計算一組數位中的平均值時會忽略 Null。

如果未指定 特定的數值運算式(通常是量值),Avg 函數會平均目前查詢內容中的每個量值。 如果提供特定的量值, Avg 函式會先評估集合上的量值,然後函式會根據指定的量值計算平均值。

注意

在匯出成員語句中使用 CurrentMember 函式時,您必須指定數值運算式,因為這類查詢內容中目前座標沒有預設量值。

若要強制包含空白資料格,應用程式必須使用 CoalesceEmpty 函式,或指定有效的 Numeric_Expression ,以提供空白值 0 的值。 如需空白儲存格的詳細資訊,請參閱 OLE DB 檔。

範例

下列範例會傳回指定集合上量值的平均值。 請注意,指定的量值可以是指定集合成員或指定量值的預設量值。

WITH SET [NW Region] AS

{[Geography].[State-Province].[Washington]

, [Geography].[State-Province].[Oregon]

, [Geography].[State-Province].[Idaho]}

MEMBER [Geography].[Geography].[NW Region Avg] AS

AVG ([NW Region]

--Uncomment the line below to get an average by Reseller Gross Profit Margin

--otherwise the average will be by whatever the default measure is in the cube,

--or whatever measure is specified in the query

--, [Measures].[Reseller Gross Profit Margin]

)

SELECT [Date].[Calendar Year].[Calendar Year].Members ON 0

FROM [Adventure Works]

WHERE ([Geography].[Geography].[NW Region Avg])

下列範例會從 Adventure Works Cube 傳回在 2003 會計年度中每個月各天計算的量值每日平均值 Measures.[Gross Profit Margin] 。 Avg 式會從階層中每個月 [Ship Date].[Fiscal Time] 包含的天數集合計算平均值。 計算的第一個版本顯示 Avg 在排除未記錄平均值任何銷售額的天數時的預設行為,第二個版本顯示如何包含沒有平均銷售額的天數。

WITH MEMBER Measures.[Avg Gross Profit Margin] AS

Avg(

Descendants(

[Ship Date].[Fiscal].CurrentMember,

[Ship Date].[Fiscal].[Date]

),

Measures.[Gross Profit Margin]

), format_String='percent'

MEMBER Measures.[Avg Gross Profit Margin Including Empty Days] AS

Avg(

Descendants(

[Ship Date].[Fiscal].CurrentMember,

[Ship Date].[Fiscal].[Date]

),

CoalesceEmpty(Measures.[Gross Profit Margin],0)

), Format_String='percent'

SELECT

{Measures.[Avg Gross Profit Margin],Measures.[Avg Gross Profit Margin Including Empty Days]} ON COLUMNS,

[Ship Date].[Fiscal].[Fiscal Year].Members ON ROWS

FROM

[Adventure Works]

WHERE([Product].[Product Categories].[Product].&[344])

下列範例會從 Adventure Works Cube 傳回量值每日平均值 Measures.[Gross Profit Margin] ,從 Adventure Works Cube 計算到 2003 會計年度每個學期的天數。

WITH MEMBER Measures.[Avg Gross Profit Margin] AS  
   Avg(  
      Descendants(  
         [Ship Date].[Fiscal].CurrentMember,   
            [Ship Date].[Fiscal].[Date]  
      ),   
      Measures.[Gross Profit Margin]  
   )  
SELECT  
   Measures.[Avg Gross Profit Margin] ON COLUMNS,  
      [Ship Date].[Fiscal].[Fiscal Year].[FY 2003].Children ON ROWS  
FROM  
   [Adventure Works]  

另請參閱

MDX 函數參考 (MDX)