Pages

Wednesday, May 18, 2011

MDX behavior in SQL Server 2008 R2 & Earlier Versions


I have been working on Sql Server Analysis Services & MDX for quite a long period. And interestingly on day to day basis I get to understand new concepts, bugs, and some interesting features. 

Few days back, I encountered with a MDX query which behaved differently in SQL Server 2008 R2 and its earlier versions. After posting my query to MSDN forum, and raising this behavior as bug @ http://connect.microsoft.com/, I got an explanation as this behavior was a design issue in SQL Server 2008 and earlier versions and got fixed in SQL Server 2008 R2.

Below is a sample MDX which behaves differently in SQL Server 2008 R2 and earlier versions: Behavior can be seen in "Adventure Works" cube.

MDX 1:
        select [Measures].[Internet Sales Amount] on 0,
        non empty [Date].[Date].[Date] on 1
        from [Adventure Works]
        where ([Customer].[Country].&[United States],[Customer].[State-Province].&[NY]&[US])

MDX 2:
        select [Measures].[Internet Sales Amount] on 0,
        non empty [Date].[Date].[Date] on 1
        from [Adventure Works]
        where ([Customer].[Country].&[United States],{[Customer].[State-Province].&[NY]&[US]})

When I ran both queries on SQL Server 2008 R2 (Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)   Apr  2 2010 15:48:46   Copyright (c) Microsoft Corporation  Developer Edition (64-bit) on Windows NT 6.0 <X64> (Build 6002: Service Pack 2) ), both queries returned same result. i.e.

                                Internet Sales Amount
November 23, 2003      $1,735.98
May 7, 2004               $2,337.27
June 19, 2004             $50.94

Then ran these queries on SQL Server 2005/SQL Server 2008 (Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86)   Mar 29 2009 10:27:29   Copyright (c) 1988-2008 Microsoft Corporation  Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3) ). They returned different result sets. i.e.

MDX 1 Output:
                                Internet Sales Amount
November 23, 2003     $1,735.98
May 7, 2004                $2,337.27
June 19, 2004             $50.94
MDX 2 Output:

MDX 2 Output:
                               Internet Sales Amount
November 23, 2003     $14,271.38
May 7, 2004                $17,992.14
June 19, 2004             $22,902.75

Reason: Microsoft Product Team described this behavior as bug in SQL Server 2005 & 2008, which has gotten fixed in SQL Server 2008 R2. As per Microsoft Team:

"The behavior in SQL Server 2005 and 2008 was a bug. Adding curly braces around a single tuple should not impact the query results in any way. A set that contains a single tuple is equivalent to the single tuple. In SQL Server 2008 R2, we fixed the bug, and produce correct results which are identical with and without the curly braces.

The nature of the bug was to ignore the tuple inside the braces because a related attribute was present in the WHERE clause of the query."





Readers: Kindly share your comments & suggestions.