MDX Essentials : MDX Operators: The Basics

Tuesday Jan 28th 2003 by William Pearson
Share:

Author Bill Pearson continues his MDX Essentials Series with a discussion of three basic operators: Braces, Comma, and Colon. After a brief review of each, we examine the syntax involved in putting it into action, and the practical use of the operator in helping us to achieve our query objectives.

Author Bill Pearson continues his MDX Essentials Series with a discussion of three basic operators: Braces, Comma, and Colon. After a brief review of each, we examine the syntax involved in putting it into action, and the practical use of the operator in helping us to achieve our query objectives.

About the Series ...

This is the third article of the series, MDX Essentials. The primary focus of this series is an introduction to the MDX language. The series is designed to provide hands-on application of the fundamentals of the Multidimensional Expressions (MDX) language, with each tutorial progressively adding features designed to meet specific real-world needs.

For more information about the series in general, as well as the software and systems requirements needed for getting the most out of the lessons included, please see the first article, MDX at First Glance: Introduction to MDX Essentials.

What We Accomplished in our Last Article

In the second article of the series, we introduced the MDX data model, together with its most basic components, tuples, axes, and sets. We focused on the composition and common uses of tuples, axes and sets, and provided hands-on exposure to these building blocks. After discussing each of the three components, we emphasized rules of syntax that related to each, providing a foundation to build upon throughout the rest of the series. Finally, we worked practice exercises to demonstrate tangible results, and to reinforce our discussions with examples.

In this lesson, MDX Operators: The Basics, we will introduce additional ways to construct tuples and sets, taking up first the most basic of the components involved. The operators we explore in this lesson will include curled braces "{}", commas "," and colons ":". With each of the operators, we will illustrate the uses and options that are available to us in constructing basic MDX queries.

Introduction to Basic Operators

In this article, we will introduce basic components involved in the building of tuples and sets. We will focus on the composition of these important building blocks, and provide hands-on exposure to their use in simple expressions that we will run to view their output. Rules of syntax will be emphasized, the aggregate body of which will provide a basis for more complex query building as we progress through the series.

This lesson will include:

  • A brief discussion of curled braces "{}", commas "," and colons ":";

  • A examination of the MDX query results we obtain in examples that use the operators under consideration.

Let's begin by discussing the most common of the MDX operators, and some of the ways that we can call upon them in the development of expressions that we can use in standalone fashion, or that we might use in more sophisticated expressions or queries, to achieve our ends.

Basic Operators: Curled Braces, Commas and Colons

We previewed the use of curled braces and commas in our last session, within our overview of sets, and in other passages of the lesson. Curled (or "curly," depending upon whom you ask) braces must be used in some situations, and simply can be used in others. We will touch on these, as well as upon the use of curled braces to set apart set expressions consistently, to make learning MDX easier. As we stated in our last section, MDX is similar to other programming languages in its uses of various operators; one of those uses is to identify sets. Among these operators are the colon and comma (used as separators between members within sets), which, along with curled braces, will form the subjects of this lesson.

Curled Braces

Discussion:

While the syntax format of the MDX SELECT statement resembles SQL syntax in numerous ways, one of the more pronounced differences is the way that MDX distinguishes sets: it surrounds members or tuples with curly braces (the { and } characters.) We are required to surround tuples with braces anytime the tuples are explicitly listed. We will see in later lessons that there are other ways to return sets; suffice it, for now, to say that when an expression uses MDX functions and operators, there is no requirement for the expression concerned to be enclosed within braces, as long as there is not a simultaneous effort to combine the set involved with other tuples. To make what is a new subject to many of us a little easier, I will strive to consistently use curled braces with any set expressions we encounter throughout the series.

Syntax

MDX uses the brace characters to "package" a set of components from a single dimension or a set of dimensions. The following example query makes use of the curly braces to specify the results we wish to see:

SELECT 
    {[Measures].[Units Shipped]} ON COLUMNS, 
    {[Store].[Store State].[CA], [Store].[Store State].[OR], [Store].[Store State].[WA]}
         ON ROWS
FROM [Warehouse]

The expression above requests the total units shipped for the stores, as summarized specifically for the states of California, Oregon and Washington. It does this by selecting three single members of the Store dimension, the states in which they reside, and returns them on the rows of a result set that might be represented as in Table 1 below.


Table 1: Result Set, Single Dimension Members Selected as Rows

Practice

We can see, through a quick exercise, some of the finer points in the use of curly braces. Let's fire up the Sample Application that we introduced in our last tutorial, and jump right in.

1.             Start the MDX Sample Application.

2.             Clear the top area (the Query pane) of any queries or remnants that might appear.

3.             Ensure that FoodMart 2000 is selected as the database name in the DB box of the toolbar.

4.             Select the Warehouse cube in the Cube drop-down list box.

The MDX Sample Application window should resemble that shown below, complete with the information from the Warehouse cube displaying in the Metadata tree (in the left section of the Metadata pane, between the Query pane at the top of the application window, and the Results pane, at the bottom.).


Illustration 1: The MDX Sample Application Window (Compressed)

We will begin with a simple illustration that involves our current topic: Let's say that an information consumer from management of the Warehouse Division wants to know the total number of all product items shipped for all USA stores in 1997.

5.             Type the following simple query into the Query pane:

-- MDX03-1:  Tutorial Query No. 1
SELECT
{([Store].[All Stores].[USA],[Product].[All Products])} ON COLUMNS,
{([Time].[1997])} ON ROWS
FROM Warehouse
WHERE ([Measures].[Units Shipped])

6.             Click Query on the top menu, and then select Run.

We see the results below, which appear in the Query pane as soon as Analysis Services fills the cells that it determines to be specified by the query.


Illustration 2: The Initial Query Results

Note: For an explanation of the comments line, together with a general discussion regarding attributes of the basic MDX query structure, see the first article of the series.

The query delivers the results that we requested.

7.             Save the query by selecting File -> Save As, and call the file MDX03-1.

Now, let's make the following alteration to demonstrate the important nature of the curly braces:

8.             Change the following lines in the query:

-- MDX03-1:  Tutorial Query No. 1

and

{([Store].[All Stores].[USA],[Product].[All Products])} ON COLUMNS,

to the following (simply changing the comment line and removing the braces in the first line under the SELECT clause):

-- MDX03-2:  Tutorial Query No.21

and

([Store].[All Stores].[USA],[Product].[All Products]) ON COLUMNS,

Leave the rest of the query in its original form. The Query pane should resemble that shown in Illustration 3 below:


Illustration 3: The Modified Query - Braces Removed

9.             Click Query on the top menu, and then select Run.

An error dialog appears, indicating that the cellset cannot be opened, as shown in the following illustration:


Illustration 4: "Unable to Open Cellset" Dialog

The reason for the error is that, when we run the query after removing the braces, we are presenting an invalid request. The tuple in the modified line needs to be enclosed in braces to be properly recognized.

10.         If it is desirable, save the query as MDX03-2.

Commas

Discussion:

The comma operator separates tuples forming a set, particularly when we find it difficult to practically define a set using a range of component members (more on ranges later).

Syntax

As we have seen in the above examples, as well as in previous examples in the series, MDX uses the comma operator to separate tuples, which define a slice of data from a cube. (Tuples are composed of an ordered collection of one member from each of one or more dimensions)

Practice

We can see, through the following example, as well as in most of the examples in this lesson, the role of the comma operator in building a set. The example below constructs a relatively tedious row in order to contrast the process with that of the operator that we discuss next.

Let's say that we have been asked to generate several measures for the California warehouse group by management for purposes of evaluating aggregate performance of the warehouses over the 1997 / 1998 measurement periods. These seven measures, used as critical success factors for the California warehouse group manager, are as follows:

  • Store Invoice
  • Supply Time
  • Warehouse Cost
  • Warehouse Sales
  • Units Shipped
  • Units Ordered
  • Warehouse Profit

We will assemble the required information by taking the following steps:

1.             Select File -> New from the top menu in the Sample Application.

2.             Type the following query into the Query pane:

-- MDX03-3:  Tutorial Query No. 3
SELECT
  {[Measures].[Store Invoice],[Measures].[Supply Time],
      [Measures].[Warehouse Cost],[Measures].[Warehouse Sales], 
      [Measures].[Units Shipped],[Measures].[Units Ordered], 
      [Measures].[Warehouse Profit]} ON COLUMNS,
  { [Time].[1997], [Time].[1998] } ON ROWS 
FROM Warehouse
WHERE
([Warehouse].[All Warehouses].[USA].[CA])

3.             Click the green Run Query button to execute the query.

The query delivers the results that were requested by the management for the California warehouse group manager's performance appraisal.


Illustration 5: The Query Results, Showing the Desired Measures

4.             Save the query as MDX03-3.

We will now examine another operator, the colon, and another way to achieve our results from the above query – but with a little less typing.

Colons

Discussion:

The colon operator provides us a means of leveraging the natural order of members to create a set. Order is important, because the levels within a dimension house their respective members either in member name or member key order. We can take advantage of the order of the members, and define sets based upon ranges within the order, by using the colon operator.

When we use the colon operator to define a set, members on both sides of the colon operator are included in the resulting set. The fact that the range selection is inclusive is a key concept.

Syntax

We can illustrate the syntax within which a colon operator is used with the following example, excerpted from our practice exercise in the immediately preceding section. The set of members specified in the set:

  {[Measures].[Store Invoice],[Measures].[Supply Time],  [Measures].[Warehouse Cost],
 [Measures].[Warehouse Sales],[Measures].[Units Shipped],
 [Measures].[Units Ordered],[Measures].[Warehouse Profit]}

can be retrieved with the following syntax:

{ [Measures].[Store Invoice]: [Measures].[Warehouse Profit]}

provided that the range specified by the colon takes into account the natural order of the members. Let's verify this point by putting it into action in the following exercise.

Practice

We can readily see the value of the colon operator in the following example, where we can use a colon to mitigate the tedium of listing the members individually and separating them by commas, as we did in the previous example. With the exception of applying the colon's "range logic" to the specification, the query and its result set are identical to that of the last exercise.

1.             Select File -> New from the top menu in the Sample Application.

2.             Type the following query into the Query pane:

-- MDX03-4:  Tutorial Query No. 4
SELECT
  { [Measures].[Store Invoice]: [Measures].[Warehouse Profit]} ON COLUMNS,
  { [Time].[1997], [Time].[1998] } ON ROWS
FROM Warehouse
WHERE
([Warehouse].[All Warehouses].[USA].[CA])

3.             Click the green Run Query button to execute the query.

As we can see above, the end members of the range of tuples that form the set defining the columns in the query are separated by the colon operator.

We observe the results, which appear in the Query pane as soon as Analysis Services fills the cells that it determines to be specified by the query. The requested measures should appear as shown in Illustration 6 below.


Illustration 6: The Query Results, after Leveraging the Economies of the Colon Operator

The more new query delivers the same results obtained before, and provides the measures that were requested by management, with more compact syntax.

4.             Save the query by as MDX03-4.

As we move into the next lessons' coverage of many of the functions available in MDX, as well as into progressively more advanced stages of query building, we will call upon the basic operators frequently. A grasp of the basic operators and functions will be vital to success in our taking advantage of the more complex MDX concepts that we will uncover. Practice with these components will assure that their use comes as second nature, and will create a foundation from which the elegance and power of MDX can be fully exploited.

Next in Our Series ...

In this article, we introduced additional ways to construct and manipulate tuples and sets, reviewing some of the most basic components involved. The operators we explored in this lesson included curled braces "{}", commas "," and colons ";". For each of these, we discussed the role it plays in MDX expressions and queries, the syntax involved in putting it into action, and the practical use of the operator in helping us to achieve our objectives.

In our next lesson, MDX Operators and Functions: The .Members Function, we will introduce a powerful function that allows us to retrieve and perform operations upon levels, hierarchies and dimensions. The .Members function lies at the core of numerous related functions, and provides what will be shown as a useful basis for many of the operations that we will undertake with MDX as a tool.


See All Articles by Columnist William E. Pearson, III


Discuss this article in the MSSQL Server 2000 Analysis Services and MDX Topics Forum.

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved