Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
» Sitemap
Free Newsletters:

By submitting your information, you agree that databasejournal.com may send you databasejournal offers via email, phone and text message, as well as email offers about other products and services that databasejournal believes may be of interest to you. databasejournal will process your information in accordance with the Quinstreet Privacy Policy.

News Via RSS Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted Nov 10, 2003

MDX Essentials: Basic Set Functions: The Union() Function - Page 2

By William Pearson


The Union() function (using either primary or alternative syntax) returns the combination of two sets' data. Use of the ALL flag in the primary syntax allows duplicate members to remain within the newly produced set, as we shall see in a step in the practice example.

We can also combine sets via one of the alternate syntaxes shown above. We can accomplish the union by enclosing the sets in a list-like manner, separating them with a comma; additionally, we can simply place a plus ("+") operator between the sets we intend to join.

According to the SQL Server 2000 Books Online, the use of either alternative approach is equivalent to the primary approach with an ALL flag in place; that is, anytime one of the alternate syntaxes is chosen, duplicates within the newly created set are retained in the set. (My experience appears to differ: I find that, for one of the alternate syntaxes, duplicates are not retained. We shall see an example of this in the practice set of the next section.)

The following example expression illustrates a use of the primary Union() function. Suppose we are asked by a group of FoodMart information consumers to present total Warehouse Sales for the state of Washington, by child city, together with the total sales of one city in Oregon, Portland, for reasons known only to management. We might approach this need with an expression similar to this:


{[Store].[All Stores].[USA].[WA].Children},

{[Store].[All Stores].[USA].[OR].[Portland]})

This expression in a proper query, for the measure Warehouse Sales, would result in the return of the set depicted in Table 1.













Walla Walla






Table 1: Results of a Union, Selecting Warehouse Sales as the Measure

In the expression above, we use the Union() function, in combination with the .Children member function (See MDX Member Functions: The "Family" Functions for a tutorial on this and related family functions) to enumerate the Washington, and Oregon (in this case, one specific city, Portland) child cities.

We will practice the use of the Union() function in the section that follows. Moreover, we will explore the use of the alternate syntaxes we have discussed, after the steps of our practice with the primary syntax, to consolidate our focus and activate the concepts that parallel those of the primary syntax.


The Basics

Let's reinforce our understanding of the basics we have covered so far, and extend those concepts to the "alternate sisters" of the primary Union() syntax. We will use the Union() function in a manner that illustrates its operation in a multi-step example that builds from a pair of simple select queries, to a combination of the two in a Union() illustration. From this point, we will investigate how to obtain similar results with the alternate syntaxes. We will call upon the MDX Sample Application again, as our tool for constructing and executing the MDX we examine, and for viewing the result datasets we obtain.

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.

Let's assume for our practice example that we have been asked to supply data for a similar need as that we instanced in the example used in the syntax section. To reiterate, we are asked to present total Warehouse Sales, a value that is captured monthly within the FoodMart organization and which is stored in the Warehouse cube, for the state of Washington, by child city, together with the total sales of one city in Oregon, Portland. We will begin by composing a simple query to select the sales figure for the Washington cites, followed by a query to select the same information for the single Oregon city. We will then combine the two with the Union() function and its alternates.

5.  Type the following query into the Query pane:

-- MDX13-1:  Tutorial Query Step 1
{[Measures].[Warehouse Sales]} ON COLUMNS,
{[Store].[All Stores].[USA].[WA].Children} ON ROWS
FROM Warehouse

The purpose of this query is to simply generate the "first half" of the pair of sets that we intend to join together in the Union() query that will follow.

6.  Execute the query by clicking the Run Query button in the toolbar.

The Results pane is populated by Analysis Services, and the dataset shown in Illustration 1 appears.

Illustration 1: Result Dataset - Initial Set to be "Unionized"

We see the total Warehouse Sales for Washington returned (actually for all years in the cube, as we do not specify that we want further detail).

7.  Select File -> Save As, name the file MDX13-1, and place it in a meaningful location.

8.  Select File --> New from the main menu.

A new, blank Query pane appears.

9.  Type the following query into the Query pane:

-- MDX13-2:  Tutorial Query Step 2


{[Measures].[Warehouse Sales]} ON COLUMNS,

{[Store].[All Stores].[USA].[OR].[Portland]} ON ROWS

FROM Warehouse

The purpose of this query is to simply generate the "second half" of the pair of sets that we intend to join together in the Union() query that we will construct next.

10.  Execute the query by clicking the Run Query button in the toolbar.

MS SQL Archives

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM