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

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

News Via RSS Feed

Rss Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted May 10, 2004

MDX Essentials: Basic Set Functions: Subset Functions: The Head() Function - Page 6

By William Pearson

Another Example

Let's construct a query to meet another hypothetical business need, and to practice what we have learned further. Let's say that Logistics Department, very pleased with the metrics that we provided in our initial example, rewards us fully in the way that only information consumers can: they contact us with a more elaborate requirement.

It seems that Logistics would like to be able to present, within a single report, the difference in total Units Shipped and Units Ordered for the first two Quarters of 1998, preferring that we label this derived metric Volume Delta. They want to see the measures for the Washington Warehouse-Cities only, at least in this request. Further, they want to see this information presented in such a way that the Units Shipped, Units Ordered and Volume Delta values are side-by-side, for easy verification of the new variance amount.

We will return to the MDX Sample Application, creating a new query to handle this request.

1. Select File --> New to create a new MDX query.

A blank Query pane appears.

2. Type the following query into the Query pane:

-- MDX019-5, Use of Head() Function - Bonus Example

   MEMBER [Measures].[Volume Delta] AS

     '[Measures].[Units Ordered] -  [Measures].[Units Shipped]'



       {HEAD([Time].[Year].[1998].Children, 2)},

           {[Measures].[Units Ordered], [Measures].[Units Shipped], 

                [Measures].[Volume Delta]} ) ON COLUMNS,

       {[WAREHOUSE].[Country].[USA].[WA].Children} ON ROWS



In the query above, we use the WITH keyword to create a calculated measure, to act as our new derived Volume Delta measure. In addition, we exploit the CrossJoin() function to order our three measures under each Quarter we present. Finally, we use the Head() function, once again, to retrieve the first two Quarters of 1998 for our column axis.

NOTE: For details concerning calculated measures, see my articles index at Database Journal.com. For information about the CrossJoin() function, see my article Basic Set Functions: The CrossJoin() Function.

3. 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 9 appears.

Illustration 9: Result Dataset

We see Q1 and Q2 populating the columns across, together with the Units Shipped, Units Ordered and Volume Delta measures aligned under each. In addition, the Washington Warehouse-Cities appear in the row axis, as required by the information consumers in Corporate Logistics.

4. Select File -> Save As, name the file MDX19-5, and place it in a meaningful location.

5. Close the Sample Application when ready.

Summary ...

This article served as the beginning of a set of three articles surrounding subset functions. We introduced the Head() function, whose general purpose is to return a specified number of elements in a set, preserving natural order. We commented upon the operation of the function, and then examined its syntax.

We undertook practice examples with the function, within which we acted to meet illustrative business requirements. We demonstrated the manner in which the Head() function handles various numeric expression input scenarios. Throughout the practice examples, we briefly discussed the results datasets we obtained with regard to the Head() function, together with other surrounding considerations.

We will continue our "triptych" surrounding subset functions in our next article, where we will examine the Tail() function. The last of these three articles will focus on the Subset() function. The objective of covering the three subset functions in close proximity is to emphasize their commonalities from the perspective of usage and operation; once we have dedicated our attention to each in turn, we will be able to more finely distinguish among them for the attributes we need to leverage to meet specific business needs.

» 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

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