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

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted Nov 10, 2003

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

By William Pearson

The behavior of the Union() function is demonstrated in its elimination of the duplicate Bellingham member - a characteristic that is the default of the primary syntax with which we have been working. We note, too, that the duplicate is deleted from the "tail"; the last of the duplicate pair is the one that is "dropped."

We can prove this to ourselves by overriding the default behavior, and using the ALL flag to allow the duplicate to be retained. To do so, we will make an adjustment to the query we created in Step 4 above.

5.  Insert a comma, and then the word "ALL" into the second set specification within the Union() function, that is:

{[Store].[All Stores].[USA].[WA].[Bellingham]})  

between the right-most curly brace and the closing parenthesis, as shown:

{[Store].[All Stores].[USA].[WA].[Bellingham]},ALL)

6.  Modify the comment line to read:

-- MDX13-5:  Tutorial Query Step 5

The Query pane appears as shown in Illustration 5.


Illustration 5: The Query with Modifications Circled

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

The Results pane is populated, and the dataset shown in Illustration 6 appears. We see the "tail" instance of the duplicate Bellingham set is retained, thanks to the insertion of the ALL flag into our Union() function.


Illustration 6: Union() Function Results - Duplicates Retained via the ALL Flag

8.  Save the file as MDX13-5.

And so we see how to retain duplicates in a simple example that illustrates the influence of the ALL flag upon the Union() function. Let's turn now to the alternate syntaxes, both to examine them in general as options we might use, and to explore their implicit default behavior with regard to handling duplicates.

9.  Select File --> New from the main menu of the Sample Application.

A blank query pane appears.

10.  Type the following query into the Query pane:

-- MDX13-6:  Tutorial Query Step 6

SELECT

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

({[Store].[All Stores].[USA].[WA].Children} +

{[Store].[All Stores].[USA].[WA].[Bellingham]})

ON ROWS

FROM Warehouse

The Union() function is called into action this time using one of two alternate syntaxes. In this approach, we use the "+" operator to effect the Union() function, with a noteworthy characteristic of this arrangement being that the UNION keyword is not used at all. Let's take a look at the results we obtain through the use of this alternate approach.

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

The Results pane is populated, and the dataset shown in Illustration 7 appears.


Illustration 8: Union() Function Results - Alternate Using "+" Operator

12.  Save the file as MDX13-6.

Although the Books Online state that "Duplicated members are always retained when an alternate syntax is used," we see clearly in the foregoing results that the duplicate has been eliminated, just as it was using the primary syntax (default scenario.) Now let's attempt the same operation with the other alternative option in a final query.

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

A blank query pane appears.

14.  Type the following query into the Query pane:

-- MDX13-7:  Tutorial Query Step 7

SELECT

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

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

{[Store].[All Stores].[USA].[WA].[Bellingham]}}

ON ROWS

FROM Warehouse 

The Union() function is enacted in this query using the second of two alternate syntaxes. In this approach, we simply list the sets inside the braces, separated by a comma. Again, we note that the UNION keyword is not used at all. Let's see how duplicates are handled when performing a union via this alternate option.

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

The Results pane is populated, and the dataset shown in Illustration 8 appears.


Illustration 8: Union() Function Results - Alternate Using the "List" Approach

16.  Save the file as MDX13-7.

In this case, we note that the duplicate, tail instance of Bellingham is retained, much as it was when using the ALL flag within the primary syntax example above.

In conclusion, we can see that the Union() function, both in its primary and alternate syntax forms, provides us the significant capability of performing combinations of sets. Our selection of an alternate syntax, or of the use of the optional ALL flag in the primary syntax, can also afford us flexibility in the handling of duplicates in queries we construct that use this function.

Summary ...

In this lesson, we explored the commonly used Union() function, whose purpose it is to combine two sets into one. We discussed the Union() function in general, and emphasized its provision for important capabilities within MDX, and its value within our analysis toolsets.

In addition to discussing the purpose and operation of the Union() function, we examined both primary and alternate syntaxes involved in its use, illustrating the application of each in practice exercises. Focusing on the treatment of duplicates in each of the syntactical approaches, we discussed the results we obtained in each exercise, remarking on distinguishing characteristics of each.

» See All Articles by Columnist William E. Pearson, III

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



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


















Thanks for your registration, follow us on our social networks to keep up-to-date