Home > SQL Server Tips > Database Management and Administration > CASE statements now supported by MDX script in SQL Server 2005
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE MANAGEMENT AND ADMINISTRATION

CASE statements now supported by MDX script in SQL Server 2005


Baya Pavliashvili, Contributor
02.23.2007
Rating: -4.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


MDX now supports CASE statements, which are considerably easier to code and maintain than the nested IIF statements we had to write with previous versions. As with many other programming languages, CASE construct is used to examine various criteria and change the flow of the MDX script accordingly. The syntax of a CASE statement resembles the same construct in Transact-SQL, as follows:


For example, we could examine the levels of a particular hierarchy and apply a different calculation for a specific measure at each level. Using an Adventure Works sample cube, the following statement could define the calculation for "growth in reseller sales":


The first clause of this expression sets the value of the calculated member to "NA" for the first member of the fiscal hierarchy within the date dimension. Obviously, you can't calculate the growth if there are no prior fiscal periods; you don't have anything to compare with the current period. Next, the expression checks whether the previous member has an empty value. Again, it doesn't make sense to compare the current period with an empty value; so we set the value of the calculated member to NULL.

Finally, if neither of the conditions is met, we must be defining growth of the customer base for a member that isn't the first member and the prior member must have a non-empty value. So we subtract the customer count of the previous member from the customer count of the current member, and divide the result by the customer count of the previous member. How would we write the same expression in the previous version of Analysis Services? Like this:


Although nested IIF syntax is still supported, you can probably tell that the first format is much cleaner than the second. Now consider what would happen if we had to examine 10 different conditions instead of two! We can examine results of both calculations using the following query:


SELECT {[date].fiscal.[month].members} ON 1, 
{measures.[reseller sales amount], 
measures.[growth in reseller sales amount],
measures.[growth in reseller sales amount IIF]} ON 0
FROM [adventure works]

As the results show, both calculations return the same values (I have abbreviated the results; the query actually returns 38 rows):

Reseller Sales AmountGrowth in reseller sales amount Growth in reseller sales amount IIF
Jul-01$489,328.58(null)(null)
Aug-01$1,538,408.31214.39%214.39%
Sep-01$1,165,897.08-24.21%-24.21%
Oct-01$844,721.00-27.55%-27.55%
Nov-01$2,324,135.80175.14%175.14%
Dec-01$1,702,944.54-26.73%-26.73%
Jan-02$713,116.69 -58.12%-58.12%
Feb-02$1,900,788.93166.55%166.55%
Mar-02$1,455,280.41-23.44%-23.44%



More powerful Analysis Services MDX in SQL Server 2005

 Introduction: Introduction
 Part 1: CASE statements now supported by MDX script
 Part 2: Sub-queries supported in Analysis Services MDX
 Part 3: MDX functions: SCOPE, THIS and FREEZE

ABOUT THE AUTHOR:   
Baya Pavliashvili is a DBA manager overseeing database operations that support more than one million users. Pavliashvili's primary areas of expertise are performance tuning, replication and data warehousing.
Copyright 2007 TechTarget

More on this topic:

  • Use cube partitions to improve Analysis Services performance
  • Analysis Services 2005 in SQL Server has improved security
  • SQL Server Business Intelligence toolbox


    Rate this Tip
    To rate tips, you must be a member of SearchSQLServer.com.
    Register now to start rating these tips. Log in if you are already a member.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



    RELATED CONTENT
    SQL Server Business Intelligence (BI) and Data Warehousing
    DBA career paths could lead to business intelligence
    Are data warehouses made for the cloud?
    Q&A: Business intelligence gets a facelift in SQL Server 2008 R2
    Project Gemini gets a new name, Madison earns buzz
    Speed up reports in SQL Server Reporting Services with caching
    Data Transformation Services vs. SSIS: The key differences
    Using package configurations in SQL Server Integration Services (SSIS)
    How SQL Server 2008 components impact SharePoint implementations
    Achieving high availability and disaster recovery with SharePoint databases
    Recommended practices for SQL Server Analysis Services aggregations

    Microsoft SQL Server 2005
    End of life comes for SQL Server 2005 SP2, 2008
    SQL Server Reporting Services Fast Guide
    SQL Server Service Broker Tutorial and Reference Guide
    Tips for tuning SQL Server 2005 to improve reporting performance
    SQL Server consolidation: Why it's an optimization technique
    Parent-child dimensions in SQL Server 2005 with Analysis Services MDX
    Enforcing data integrity in a SQL Server database
    SSIS error message due to installation problem on SQL Server 2005
    Should you upgrade to SQL Server 2005 or SQL Server 2008?
    Basics for working with DATETIME and SMALLDATETIME in SQL Server 2005
    Microsoft SQL Server 2005 Research

    Database Management and Administration
    Meet compliance requirements with improved database security practices
    Hardening the network and OS for SQL Server security
    Securing the server and database in SQL Server
    How SQL Server 2008 components impact SharePoint implementations
    Troubleshooting Distributed Transaction Coordinator errors in SQL Server
    Achieving high availability and disaster recovery with SharePoint databases
    Clearing the Windows page file and its effect on server performance
    Deploying a SQL Server virtual appliance for Microsoft Hyper-V
    How to create SQL Server virtual appliances for Hyper-V
    Push vs. pull: Configuring SQL Server replication

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    data aggregation  (SearchSQLServer.com)
    data preprocessing  (SearchSQLServer.com)
    data warehouse  (SearchSQLServer.com)
    FileMaker  (SearchSQLServer.com)
    GIS  (SearchSQLServer.com)
    MOLAP  (SearchSQLServer.com)
    pivot table  (SearchSQLServer.com)
    Quiz: SQL Server 2000  (SearchSQLServer.com)
    SQL  (SearchSQLServer.com)
    T-SQL  (SearchSQLServer.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • SQL Server Development - .NET, C#, T-SQL, Visual Basic
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts