IIf(MDX)

根据布尔条件是真还是假,评估不同的分支表达式。

Syntax

  
IIf(Logical_Expression, Expression1 [HINT <hints>], Expression2 [HINT <hints>])  

Arguments

IIf 函数有三个参数:iif(<condition>, <然后分支>, <else 分支>)。

Logical_Expression
一个评估为 (1)或 (0)的条件。 它必须是一个有效的多维表达式(MDX)逻辑表达式。

表达1提示 [渴望|严格|懒惰]]
当逻辑表达式值为 时,使用该词。 Expression1 必须是有效的多维表达式(MDX)表达式。

表达2提示 [渴望|严格|懒惰]]
当逻辑表达式值为 时使用。 Expression2 必须是有效的多维表达式(MDX)表达式。

Remarks

当该逻辑表达式的值为零时,该条件被评估为 。 其他任何值都被评为

当条件为 时, IIf 函数返回第一个表达式。 否则,函数返回第二个表达式。

指定的表达式可以返回数值或MDX对象。 此外,指定的表达式不必在类型上匹配。

不推荐用 IIf 函数来基于搜索条件创建成员集合。 相反,使用 筛选 函数对指定集合中的每个成员进行逻辑表达式的评估,并返回一个成员子集。

注释

如果任一表达式都值为NULL,满足该条件时结果集将为NULL。

提示是一个可选的修饰符,用于决定表达式如何以及何时被评估。 它允许你通过指定表达式的评估方式来覆盖默认查询计划。

  • EAGER 在原始 IIF 子空间上评估表达式。

  • STRICT仅在逻辑条件表达式创建的受限子空间中求值表达式。

  • LAZY以细胞为单位评估表达。

EAGER和STRICT仅适用于IIF的then-else分支,而LAZY则适用于所有MDX表达式。 任何MDX表达都可以进行HINT LAZY检测,以逐细胞模式评估该表达。

在提示中,EAGER和STRICT是互斥的;它们可以在同一个IIF(,,)中用于不同的表达式。

欲了解更多信息,请参阅 SQL Server Analysis Services 2008 中的 IIF 函数查询提示,以及 MDX IIF 函数和 CASE 语句的执行计划和计划提示

示例

以下查询展示了在计算度量中简单使用 IIF 的方法,当度量互联网销售金额大于或小于10000美元时,返回两种不同字符串中的一个:

WITH MEMBER MEASURES.IIFDEMO AS  
IIF([Measures].[Internet Sales Amount]>10000  
, "Sales Are High", "Sales Are Low")  
SELECT {[Measures].[Internet Sales Amount],MEASURES.IIFDEMO} ON 0,  
[Date].[Date].[Date].MEMBERS ON 1  
FROM [Adventure Works]  

IIF的一个非常常见的用途是处理计算度量内的“除以零”误差,如下示例所示:

WITH  
//Returns 1.#INF when the previous period contains no value  
//but the current period does  
MEMBER MEASURES.[Previous Period Growth With Errors] AS  
([Measures].[Internet Sales Amount]-([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER))  
/  
([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER)  
,FORMAT_STRING='PERCENT'  
//Traps division by zero and returns null when the previous period contains  
//no value but the current period does  
MEMBER MEASURES.[Previous Period Growth] AS  
IIF(([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER)=0,  
NULL,  
([Measures].[Internet Sales Amount]-([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER))  
/  
([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER.PREVMEMBER)  
),FORMAT_STRING='PERCENT'  
SELECT {[Measures].[Internet Sales Amount],MEASURES.[Previous Period Growth With Errors], MEASURES.[Previous Period Growth]} ON 0,  
DESCENDANTS(  
[Date].[Calendar].[Calendar Year].&[2004],  
[Date].[Calendar].[Date])  
ON 1  
FROM [Adventure Works]  
WHERE([Product].[Product Categories].[Subcategory].&[26])  

以下是 IIF 在生成函数中返回两个集合之一以创建行上复元组的示例:

SELECT {[Measures].[Internet Sales Amount]} ON 0,  
//If Internet Sales Amount is zero or null  
//returns the current year and the All Customers member  
//else returns the current year broken down by Country  
GENERATE(  
[Date].[Calendar Year].[Calendar Year].MEMBERS  
, IIF([Measures].[Internet Sales Amount]=0,  
{([Date].[Calendar Year].CURRENTMEMBER, [Customer].[Country].[All Customers])}  
, {{[Date].[Calendar Year].CURRENTMEMBER} * [Customer].[Country].[Country].MEMBERS}  
))  
ON 1  
FROM [Adventure Works]  
WHERE([Product].[Product Categories].[Subcategory].&[26])  

最后,这个例子展示了如何使用计划提示:

WITH MEMBER MEASURES.X AS  
IIF(  
[Measures].[Internet Sales Amount]=0  
, NULL  
, (1/[Measures].[Internet Sales Amount]) HINT EAGER)  
SELECT {[Measures].x} ON 0,  
[Customer].[Customer Geography].[Country].MEMBERS ON 1  
FROM [Adventure Works]