返回由逻辑测试确定的两个值之一。
对于由逻辑表达式指定的表达式,仅当其值为零时,此表达式的计算结果才为 false。其他任何情况都取值为 true。
如果指定的逻辑表达式计算结果为 true,则 IIf 函数返回第一个表达式。否则,该函数返回第二个表达式。
指定的表达式可以返回值或 MDX 对象。此外,指定表达式的类型无需匹配。
注意: |
---|
在 Microsoft SQL Server 2000 中,Analysis Services 仅支持数值和字符串返回类型,并且指定表达式的类型必须相同。这些限制不再适用于 SQL Server Analysis Services。
|
建议不要用 IIf 函数来创建基于搜索条件的成员集。请改用 Filter 函数使用逻辑表达式对指定集中的每个成员求值,然后返回成员子集。
注意: |
---|
如果任意一个表达式的计算结果为 NULL,则当满足条件时,结果集为 NULL。
|
以下查询说明 IIF 在计算度量值内部的简单用法,该函数在度量值 Internet Sales Amount 大于或小于 $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 的一个示例,在 Generate 函数内部返回两个集之一,以便在行上创建一个复杂的元组集:
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]