MDX Queries

MDX queries are to SQL Server Analysis Services (SSAS) multidimensional data as T-SQL queries are to relational data. Although the syntax of a dimensional query varies from that of a relational query, working with MDX queries can become as comfortable as working with T-SQL queries over time.

To illustrate, let’s look at how to code a simple MDX statement to query data from the SSAS cube created in the articles “Creating Dimensions in SSAS, Part I,” InstantDoc ID 98510 and “Creating Dimensions in SSAS, Part II,” InstantDoc ID 98699.

Before creating the MDX statement, you need to know a couple of key syntax elements for MDX. A basic MDX structure takes the format of

Select <row_axis, mdx_set,> on Columns,

     <column_axis, mdx_set,> on Rows

From <from_clause, mdx_name,>
Where <where_clause, mdx_set,>

This example starts with a Select statement that should look vaguely familiar to ADO.NET developers. The <row_axis, mdx_set,> defines the first dimension, for columns. A cube allows you to reference how data behaves across its dimensions; the first part of the Select statement defines a dimension that will form the horizontal axis of a grid. By laying out these elements horizontally, you define the columns of that grid, as specified by the on Columns qualifier.

The on Columns qualifier is followed by the definition of the second dimension, for rows. This is the vertical dimension of your grid, which defines the rows of that grid, as specified by the on Rows qualifier. (You can also use an MDX query to define other types of structures; this example introduces the MDX syntax for only the Select statement. For articles that give you more information on MDX, see the web Learning Path at InstantDoc ID 99908.)

Next, the From clause names the location the data is retrieved from. In an MDX query, this location is typically the name of the cube you’re using. Finally, the Where clause defines the data that will be applied. Most SQL developers see a Where clause as a way to exclude results. The MDX Where clause allows you to screen by identifying the data that should be included. You arrive at a similar end result with T-SQL. Think of screening that explicitly includes the data that meets a specified condition. For most MDX queries, the condition is simply that it exists in a given location.

When you understand how a simple query description works, you can try your hand at creating a query. The most basic method is to type the complete MDX query with its specific dimension- and fact-related information. However, you can also do this using a toolset with a graphical UI, such as the SQL Server 2005 Data Mining Add-ins for Microsoft Office 2007. The Data Mining Client for Excel allows you to graphically add data elements and generate the resulting MDX query. You can use this tool to build your MDX query and then copy and paste that query into your custom application. More information on this package and a link to the download is available at www.microsoft.com/sql/technologies/dm/addins.mspx.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
Vision road sign
May 6, 2013
blog

Cheaters Never Win, Even in TPC Benchmarks

In this portion of the series on database benchmarking, I want to tell you about one of my favorite aspects of the TPC benchmarks – CHEATING....More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.