应用RANK时忽略null的优雅方法

时间:2013-12-17 18:15:23

标签: mdx

本文提供了一种在应用RANK时忽略null的方法:http://www.bidn.com/blogs/CraigLove/ssas/2617/mdx-walkthrough

有没有比使用CASE更优雅的方式?

WITH
    MEMBER [Measures].[City Rank] 
AS
    CASE
        WHEN 
            NOT ISEMPTY
            (
                (
                [Geography].[City].CurrentMember
                ,[Measures].[Reseller Sales Amount]
                )
            )
        THEN
            RANK
                (
                [Geography].[City].CurrentMember
                ,[Geography].[City].AllMembers
                ,[Measures].[Reseller Sales Amount]
                ) 
        ELSE
            NULL
    END     


SET [OrderedCity]
    AS 
            ORDER
            (
            [Geography].[City].AllMembers
            ,[Measures].[Reseller Sales Amount]
            ,DESC
            )
SELECT 
    {
    [Measures].[City Rank] 
    ,[Measures].[Reseller Sales Amount]
    } ON COLUMNS
    ,NON EMPTY [OrderedCity] ON ROWS
FROM [Adventure Works]; 

1 个答案:

答案 0 :(得分:1)

您可以在NonEmpty中应用OrderedCity功能:

WITH

SET [OrderedCity]
    AS 
            NonEmpty(
            ORDER
            (
            [Geography].[City].AllMembers
            ,[Measures].[Reseller Sales Amount]
            ,DESC
            )
            )
    MEMBER [Measures].[City Rank] 
AS
            RANK
                (
                [Geography].[City].CurrentMember
                ,[OrderedCity]
                ) 

SELECT 
    {
    [Measures].[City Rank] 
    ,[Measures].[Reseller Sales Amount]
    } ON COLUMNS
    ,[OrderedCity]
     ON ROWS
FROM [Adventure Works]; 

这样,NON EMPTY在它放在那里之前就被应用在行上的集合上,因此不需要在行本身上应用它。此外,在City Rank的定义中重用集合允许Analysis Services仅计算一次排序,并缓存有序集。否则,Rank将要求为每一行重新计算排序。