平均值(MDX)

評估集合並回傳集合中非空格值的平均值,該值是對集合內測度或指定測度的平均值。

Syntax

  
Avg( Set_Expression [ , Numeric_Expression ] )  

論點

Set_Expression
一個有效的多維表達式(MDX)表達式,回傳一組

Numeric_Expression
一個有效的數值表達式,通常是多維表達式(MDX)的儲存格座標表達式,回傳一個數字。

備註

若指定空元組集合或空集合, Avg 函數會回傳空值。

Avg 函數計算指定集合中非空格值的平均值,方法是先計算該集合中各格子的值總和,然後將計算出的總和除以該集合中非空格子的數量。

備註

分析服務在計算一組數字的平均值時會忽略空值。

若未指定特定數值表達式(通常是測度), Avg 函數會在當前查詢情境中平均每個測度。 若提供特定測度, Avg 函數先評估該測度,然後根據指定測度計算平均值。

備註

在計算成員語句中使用 CurrentMember 函式時,必須指定數值表達式,因為在此類查詢情境中,目前座標沒有預設測度。

若要強制包含空儲存格,應用程式必須使用 CoalesceEmpty 函式,或指定一個有效的 Numeric_Expression ,該 對空值提供 0 的值。 欲了解更多關於空格的資訊,請參閱 OLE DB 文件。

Examples

以下範例回傳指定集合上測度的平均值。 請注意,指定的測度可以是指定集合成員的預設測度,也可以是指定的測度。

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])  

以下範例回傳了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])  

以下範例回傳了 2003 財政年度中每個學期天數計算出的每日平均 Measures.[Gross Profit Margin] 值,該數據來自 Adventure Works 立方體。

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]