2015-02-05 152 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