2015-02-05 156 views
1

我對Nonempty()函數的理解是它需要第一個表達式,將它應用到第二個表達式上,並刪除那些在第二個表達式中沒有值的表達式。mdx非空排除存在的值

WITH 
SET [Date Range] AS 
    Filter(
     [Date].[Date].[Date], 
     [Date].[Date].CurrentMember.Member_Value >= CDate(@StartDateParam) AND 
     [Date].[Date].CurrentMember.Member_Value <= CDate(@EndDateParam) 
    ) 

MEMBER [Measures].[DateValue] AS 
    [Date].[Date].CurrentMember.Member_Value 

SELECT 
{ 
    [Measures].[DateValue], 
    [Measures].[Work Item Count] 
} ON COLUMNS, 
{ 
    CrossJoin(
     [Date Range], 
     --NonEmpty(
      [Work Item].[System_State].[System_State] 
     -- ,[Measures].[Work Item Count] 
     --) 
    ) 
} ON ROWS 
FROM [Team System] 

它返回

Date  | State | DateValue | WorkItemCount 
2/1/2015 | Active | 2/1/2015.. | 2 
2/1/2015 | Resolved | 2/1/2015.. | (null) 
2/2/2015 | Active | 2/2/2015.. | 1 
2/2/2015 | Resolved | 2/2/2015.. | 1 
2/3/2015 | Active | 2/3/2015.. | 0 
2/3/2015 | Resolved | 2/3/2015.. | 2 

當我去掉上面的非空代碼, 我得到:

Date  | State | DateValue | WorkItemCount 
2/1/2015 | Resolved | 2/1/2015.. | (null) 
2/2/2015 | Resolved | 2/2/2015.. | 1 
2/3/2015 | Resolved | 2/3/2015.. | 2 

我期待得到:

Date  | State | DateValue | WorkItemCount 
2/1/2015 | Active | 2/1/2015.. | 2 
2/2/2015 | Active | 2/2/2015.. | 1 
2/2/2015 | Resolved | 2/2/2015.. | 1 
2/3/2015 | Resolved | 2/3/2015.. | 2 

這裏發生了什麼?

出現這種情況的SQL Server 2014所以在SSAS MDX NonEmpty issue回答不適用

回答

0

儘量保持簡單。
只需推動nonEmpty了一下:

WITH 
SET [Date Range] AS 
    Filter(
     [Date].[Date].[Date], 
     [Date].[Date].CurrentMember.Member_Value >= CDate(@StartDateParam) AND 
     [Date].[Date].CurrentMember.Member_Value <= CDate(@EndDateParam) 
    ) 

MEMBER [Measures].[DateValue] AS 
    [Date].[Date].CurrentMember.Member_Value 

SELECT 
{ 
    [Measures].[DateValue], 
    [Measures].[Work Item Count] 
} ON COLUMNS, 
{ 
    NonEmpty(
     [Date Range] 
     * [Work Item].[System_State].[System_State] 
     ,[Measures].[Work Item Count] 
    ) 
} ON ROWS 
FROM [Team System] 

編輯

@George發佈了一個有趣的方法。他使用了一個輔助手段,然後輸入HAVING條款。這個條款很快,應儘可能使用,但我認爲創建一個新措施的開銷取消了性能增益HAVING。因此,可以使用類似於在這個崗位由克里斯·韋伯的腳本一個簡單的例子,以及保持它的簡單和使用NonEmpty ..

我測試過2006年:https://cwebbbi.wordpress.com/2006/01/04/the-having-clause/

--takes 17 secs 
WITH 
    MEMBER [Measures].[hasQuant] AS 
    IIF 
    (
     IsEmpty([Measures].[Internet Order Quantity]) 
    ,0 
    ,1 
    ) 
SELECT 
    [Measures].[Internet Order Quantity] ON 0 
,{ 
     [Date].[Date].MEMBERS* 
     [Product].[Subcategory].MEMBERS* 
     [Geography].[Country].MEMBERS* 
     [Customer].[Gender].MEMBERS 
    } HAVING 
    [Measures].[hasQuant] = 1 ON 1 
FROM [Adventure Works]; 

而只是保持簡單與NonEmpty

-- secs 11 secs 
SELECT 
    [Measures].[Internet Order Quantity] ON 0 
,NonEmpty 
    (
     [Date].[Date].MEMBERS* 
     [Product].[Subcategory].MEMBERS* 
     [Geography].[Country].MEMBERS* 
     [Customer].[Gender].MEMBERS 
    ,[Measures].[Internet Order Quantity] 
) ON 1 
FROM [Adventure Works]; 
0

NonEmpty()只刪除那些交叉連接的結果,讓null*null。當您遇到類似Resolved*null的情況時,NonEmpty不起作用。此外,這種方法不會給你最好的表現。試試這個:

WITH 
SET [Date Range] AS 
Filter(
    [Date].[Date].[Date], 
    [Date].[Date].CurrentMember.Member_Value >= CDate(@StartDateParam) AND 
    [Date].[Date].CurrentMember.Member_Value <= CDate(@EndDateParam) 
) 

MEMBER [Measures].[DateValue] AS 
[Date].[Date].CurrentMember.Member_Value 

// add member to filter out null 
// Work Item Count condition 
member hasState as (iif(ISEMPTY([Measures].[Work Item Count]), 0, 1)) 

SELECT 
{ 
    [Measures].[DateValue], 
    [Measures].[Work Item Count] 
} ON COLUMNS, 
{ 

     [Date Range] * 
      [Work Item].[System_State].[System_State].members 
} 
having [Measures].[hasState] = 1 
ON ROWS 
FROM [Team System] 
+0

...這是性能增益確定嗎? 'hasState'內的'iif'會有一些開銷。將'AdvWks'中的一個好樣本嘲笑起來會很有趣。也許用',null,1))替換',0,1))''可能會進一步提高速度。 – whytheq 2015-02-06 14:17:00

+0

什麼時候這部分條件是真的? 'ISEMPTY([Work Item]。[System_State] .currentmember.membervalue)' – whytheq 2015-02-06 14:21:10

+0

當您有空成員值時,如果它在尺寸設置中被允許。我評論說,你可以把它拿出來,相當少見的情況 – George 2015-02-06 14:24:18