IIf (MDX)

根據 Boolean 條件為 true 或 false 來評估不同的分支運算式。

語法

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

引數

IIf 函數會採用三個引數:iif(<condition>、<then branch>、<else branch>)。

  • Logical_Expression
    評估為 true (1) 或 false (0) 的條件。 它必須是有效的多維度運算式 (MDX) 邏輯運算式。

  • Expression1 Hint [Eager|Strict|Lazy]]
    在邏輯運算式評估為 true 時使用。 Expression1 必須是有效的多維度運算式 (MDX) 運算式。

  • Expression2 Hint [Eager|Strict|Lazy]]
    在邏輯運算式評估為 false 時使用。 Expression2 必須是有效的多維度運算式 (MDX) 運算式。

備註

只有在此運算式的值為零時,由邏輯運算式指定的條件才會評估為 false。 其他值都會評估為 true。

如果條件為 true,IIf 函數會傳回第一個運算式。 否則,此函數會傳回第二個運算式。

指定的運算式可以傳回值或 MDX 物件。 而且,指定的運算式不需要類型相符。

不建議您使用 IIf 函數根據搜尋條件建立成員集合。 而應改用 Filter 函數對指定集合中的每個成員驗算邏輯運算式,並傳回成員子集。

[!附註]

如果任何一個運算式評估為 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 簡單用法,以便在 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 函數內傳回兩個集合的其中一個,以便在資料列上建立一組複雜 Tuple:

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]

請參閱

參考

MDX 函數參考 (MDX)