计算集合,返回集合中非空单元格值的平均值,这些值是对集合内各测度或指定测度的平均值。
Syntax
Avg( Set_Expression [ , Numeric_Expression ] )
Arguments
Set_Expression
一个有效的多维表达式(MDX)表达式,返回集合
Numeric_Expression
一个有效的数值表达式,通常是多维表达式(MDX)表达式,表示返回一个数字的单元坐标。
Remarks
如果指定了空元组集合或空集合, Avg 函数返回空值。
Avg函数通过先计算指定集合中各单元格值的和,然后将计算出的总和除以指定集合中非空单元格的数量,从而计算出非空单元格值的平均值。
注释
分析服务在计算一组数字的平均值时忽略空值。
如果未指定特定的数值表达式(通常是测度), Avg 函数会在当前查询上下文中对每个测度进行平均。 如果提供了特定的测度, Avg 函数首先对该测度在集合上进行评估,然后该函数根据指定的测度计算平均值。
注释
在计算成员语句中使用 CurrentMember 函数时,必须指定数值表达式,因为在此类查询上下文中当前坐标不存在默认测度。
为了强制包含空单元格,应用程序必须使用 CoalesceEmpty 函数或指定一个有效的 Numeric_Expression ,使空值为零(0)。 有关空单元的更多信息,请参阅 OLE 数据库文档。
示例
以下示例返回指定集合上测度的平均值。 注意,指定的测度可以是指定集合成员的默认测度,也可以是指定的测度。
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财年每个月天数中从Adventure Works立方体计算出的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财年每个学期天数中从Adventure Works立方体计算出的Measures.[Gross Profit Margin]该指标的每日平均值。
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]