Home > Articles > Data > SQL Server

  • Print
  • + Share This
Like this article? We recommend

Working with Member Sets

We can generate far more sophisticated queries by adding sets of members to one or both of the axes. Let's place a set on an axis as a means of generating a display of more than one cell in our results:

  1. In the Query pane, highlight the query you just created.

  2. Choose Edit, Copy (or press Ctrl+C) to copy the original query.

  3. Choose Query, New (or click the New Query button) to set up a new Query pane.

  4. Choose Edit, Paste (or press Ctrl+V) to paste the original query into the new Query pane, for use as a "boilerplate" to begin the next query.

  5. Position the cursor immediately after the word SELECT and press the spacebar.

  6. Click the plus (+) sign next to the Set folder in the Syntax Examples list on the right side of the Metadata pane. This action will expand the Set folder.

  7. Double-click the Members-Level function to include the function in the query, following the SELECT statement (see Figure 22).

    Figure 22Figure 22 Building the query: inserting a set function.

  8. Click the <<Level>> token to select it.

  9. Expand the Stores dimension in the Metadata tree to expose its member levels.

  10. Double-click the Store State level within the expanded Stores dimension.

  11. Position the cursor immediately after the word Members, press the spacebar, and type ON COLUMNS (see Figure 23).

    Figure 23Figure 23 The complete query.

  12. Click the Run Query button. Figure 24 shows the results.

    Figure 24Figure 24 The results of the query.

    The Store State labels appear as column headings. To more closely replicate a feature of the PivotTable list, move the labels to the row axis by clicking the Pivot Results button (see Figure 25) on the sample application toolbar. Figure 26 shows the new format.

    Figure 25Figure 25 The Pivot Results button from the sample application toolbar.

    Figure 26Figure 26 Column labels are converted to row labels.

You may wonder why the query seems to be ignoring the ON COLUMNS instructions. The explanation (without going into too much detail) is the fact that an MDX query with only one axis requires that the single axis be a column axis.

Now let's add a set to the second axis in our report:

  1. Add a comma and a space after the word COLUMNS in the query.

  2. If necessary, expand the Set folder in the Syntax Examples list.

  3. Double-click the Children function. The statement should now look like this:

    SELECT [Store].[Store State].Members ON COLUMNS, «Member».Children FROM Sales
  4. Click the <<Member>> token to select it.

  5. Double-click the Store Type dimension in the Metadata tree to select it into the <<Member>> token. Here's how the statement should look:

    SELECT [Store].[Store State].Members ON COLUMNS, [Store Type].Children FROM Sales
  6. Position the cursor after Children in the statement. Press the spacebar and then type the words ON ROWS. The statement should look like this:

    SELECT [Store].[Store State].Members ON COLUMNS, [Store Type].Children ON ROWS FROM Sales

    The ON ROWS addition specifies that the second set will be added to the row axis. This can be a little confusing. When you create two axes, you must designate one as the column axis and one as the row axis. Order in the query is not as important as the fact that the designation itself must be made. The Pivot Results functionality is still in place, so in effect we're still operating under the "swap" conditions imposed on the earlier, single-set query.

  7. Click the Run Query button to execute the query (see Figure 27).

    Figure 27Figure 27 The results of the two-axis query.

  8. Save the query as MDX04-2.MDX.

    In addition to using other member functions (you can explore these in the Set folder of the Syntax Examples list), we can make a simple alteration to the query statement to cause it to return the entire membership of the Store dimension.

  9. Remove [Store State]. from the query. Then type the following text, in a line above the query statement:

    -- Store Dimension Membership

    Two forward slashes (//) work in the same way as two hyphens (--) in this comment-line notation, meaning "ignore the rest of this line for purposes of query execution." To perform the same thing for a comment that extends beyond a single line, or that consists of only a partial line, use /* at the beginning of the comment, and */ at the end. For example, the following comment would easily extend beyond a single line. We would enclose it between the /* and */ characters, as shown here:

    /* The purpose of the following query is to generate sales values
    for each of the stores, then to compare the actual amounts to the
    budgeted amounts to compute a variance value. */

    The keyword or description at the top line of a query appears in the Queries selector box in the toolbar. This is helpful when moving quickly between queries (or attempts at queries during design) for development or other purposes.


    Add comments to any but the simplest queries to make it easier to understand the logic, the objectives in creating the query, and a myriad of other such facts. Such comments are very helpful when returning to the query after time has passed.

    Figure 28 shows the modified statement in the Query pane.

    Figure 28Figure 28 The modified query with a new comment line.

  10. Click the Run Query button to execute the query. Figure 29 shows partial results.

    Figure 29Figure 29 The store dimension's entire population is retrieved.

  11. Save the query as MDX04-3.MDX.

  • + Share This
  • 🔖 Save To Your Account