IIf (MDX)
論理テストによって判別される 2 つの値の 1 つを返します。
構文
IIf(Logical_Expression, Expression1, Expression2)
引数
Logical_Expression
true または false に評価される有効な多次元式 (MDX) 論理式です。Expression1
有効な多次元式 (MDX) 式です。Expression2
有効な多次元式 (MDX) 式です。
説明
論理式で指定した式は、式の値が 0 の場合のみ false と評価されます。他のすべての値は、true と評価されます。
指定した論理式が true と評価される場合、IIf 関数は 1 番目の式を返します。それ以外の場合は、2 番目の式を返します。
指定した 2 つの式は値または MDX オブジェクトを返すことができます。また、1 番目と 2 番目の式の型は一致しなくてもかまいません。
注 |
---|
Microsoft SQL Server 2000 では、Analysis Services がサポートしていた戻り値のデータ型は数値と文字列だけなので、指定した式の型が一致している必要がありました。これらの制限事項は、SQL Server Analysis Services には適用されません。 |
検索条件に基づいてメンバーのセットを作成する場合、IIf 関数を使用することはお勧めできません。代わりに、Filter 関数を使用して、指定されたセットの各メンバーを論理式に照らして評価し、メンバーのサブセットを返すようにしてください。
注 |
---|
いずれかの式が NULL と評価される場合、その条件が満たされる場合は結果セットが NULL になります。 |
例
次のクエリでは、計算されるメンバー内で IIF を使用して、Internet Sales Amount メジャーが 10,000 米ドルを上回るか下回る場合に異なる 2 つの文字列値のいずれかを返す簡単な方法を示します。
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 の一般的な使用方法では、次の例に示すように、計算されるメンバー内で "0 除算" エラーを処理します。
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 関数内で 2 つのセットのうちいずれかを返して行に複雑な組のセットを作成します。
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]