MDX查询中的intersect和topcount以及order函数

时间:2013-08-28 09:46:48

标签: sql sql-server-2008-r2 ssas mdx olap

我使用SQL Server 2008 R2并使用SSRS,并使用Adventure Work数据库。

我为2003年,2004年两年排名前十的城市写了这个MDX查询。

with 
set [Best Cities in CY 2003/2004] as
    intersect(
        order(
            topcount(
                [Customer].[Customer Geography].[City],
                10,
                (
                    [Measures].[Internet Sales Amount],
                    [Date].[Calendar].[Calendar Year].[CY 2003]
                )
            ),
            [Measures].[Internet Sales Amount],
            bdesc
        ),
        order(
            topcount(
                [Customer].[Customer Geography].[City],
                10,
                (
                    [Measures].[Internet Sales Amount],
                    [Date].[Calendar].[Calendar Year].[CY 2004]
                )
            ),
            [Measures].[Internet Sales Amount],
            bdesc
        )
    )
Select [Measures].[Internet Sales Amount] on columns,
       [Best Cities in CY 2003/2004] on rows
From [Adventure Works]
Where 
{
    [Date].[Calendar].[Calendar Year].[CY 2003],
    [Date].[Calendar].[Calendar Year].[CY 2004]
}

但我希望得到的城市列表显示,与去年相比,互联网销售额下降了35%,同时也是同一城市中排名前10位的城市。

我怎么能得到这个结果?

1 个答案:

答案 0 :(得分:3)

您可以使用Filter

with 
set [Best Cities in CY 2003/2004] as
    filter(
        intersect(
            topcount(
                [Customer].[Customer Geography].[City],
                10,
                (
                    [Measures].[Internet Sales Amount],
                    [Date].[Calendar].[Calendar Year].[CY 2003]
                )
            ),
            topcount(
                [Customer].[Customer Geography].[City],
                10,
                (
                    [Measures].[Internet Sales Amount],
                    [Date].[Calendar].[Calendar Year].[CY 2004]
                )
            )
        ),
        ([Measures].[Internet Sales Amount], [Date].[Calendar].[Calendar Year].[CY 2004])
        /
        ([Measures].[Internet Sales Amount], [Date].[Calendar].[Calendar Year].[CY 2003])
        - 1.0
        < -0.35
    )
Select [Measures].[Internet Sales Amount] on columns,
       [Best Cities in CY 2003/2004] on rows
From [Adventure Works]
Where 
{
    [Date].[Calendar].[Calendar Year].[CY 2003],
    [Date].[Calendar].[Calendar Year].[CY 2004]
}