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 Jun 14, 2004

MDX Essentials: Basic Set Functions: Subset Functions: The Tail() Function - Page 5

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, returns with the more elaborate requirement we saw in our examination of the Head() function.



To reiterate with the focus on the Tail() function, Logistics would like to be able to present, within a single report, the difference in total Units Shipped and Units Ordered for the last 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:


-- MDX020-5, Use of Tail() Function - Bonus Example
WITH 

   MEMBER [Measures].[Volume Delta] AS

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

SELECT

   CROSSJOIN (

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

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

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

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

FROM

    [Warehouse]

In the query above, we use the WITH keyword to create a calculated measure, to act as our newly 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 Tail() function, once again, to retrieve the last 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 Q3 and Q4 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 MDX020-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 Tail() 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. In our example set, we intentionally replicated, to a large extent, the requirements we simulated in working with the Head() function in our last article, so as to compare the Tail() and Head() functions, and to note their similarities in operation, as well as to contrast the results datasets they returned.

We demonstrated the manner in which the Tail() function handles various numeric expression input scenarios, again, in contrast to the Head() function. Throughout the practice examples, we briefly discussed the results datasets we obtained with regard to the Tail() function, together with other surrounding considerations.

We will continue our "triptych" surrounding subset functions in our next article, where we will examine the Subset() function. We will again emphasize the commonalities between SubSet() and the Head() and Tail() functions, from the perspective of usage and operation. We will thus conclude our examination of the group of three subset functions, having dedicated our attention to each in turn, then comparing / contrasting them in order to finely distinguish among them for the particular characteristics we need 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

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