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 Oct 13, 2003

MDX Essentials: Basic Set Functions: The Order() Function - Page 3

By William Pearson

Practice - Returning Hierarchized Data

Let's reinforce our understanding of the basics we have covered so far, and by using the Order() function in a manner that illustrates its operation in returning hierarchized data, one of the two general options that we have in order types. 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.

We will compose a simple query to gain an understanding of the use of the Order() function to return hierarchized data, or data sorted within hierarchical considerations. Our query will focus on Warehouse Cost, a value that is captured monthly within the FoodMart organization and which is stored in the Warehouse cube.

5.  Type the following query into the Query pane:

-- MDX12-1:  Tutorial Query No. 1
SELECT 
{[Measures].[Warehouse Cost]} ON COLUMNS,
{ ORDER({[Warehouse].[USA].[WA].Children, [Warehouse].[USA].[OR].Children},  
   ([Measures].[Warehouse Cost]) , DESC)}  ON ROWS
FROM Warehouse
WHERE ([Time].[Year].[1998])

Notice that the query we have input contains an Order() expression similar to the one we used in the previous section. The query also accomplishes a complementary objective: By selecting two separate state levels in the Warehouse dimension to populate the row axis in the query, we have constructed a scenario whereby we know that we have two separate hierarchical levels; we thus have a basis for confirming the real effects of using each of the hierarchical and nonhierarchical sort types.

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

Analysis Services populates the Results pane, presenting the dataset shown in Illustration 1.


Illustration 1: Result Dataset - Order() Expression in Place, Hierarchized, Descending

We see the total Warehouse Cost for the specified states returned for year 1998. The Order() function sorts our states (Washington and Oregon are specified in the query to populate the row axis) from "highest to lowest," (and thus "from Washington to Oregon"), respecting state groupings in those sorts. We obtain the seven Washington city-children, sorted highest to lowest, with respect to Warehouse Cost, followed by the two Oregon city-children, also sorted highest to lowest based upon Warehouse Cost in descending order.

7.  Select File -> Save As, and give the file a meaningful name and location, if it is desirable to save the query.

Practice - Returning Nonhierarchized Data

Let's take a look at the same operation with a "break hierarchy" sort type, BDESC, within an otherwise identical query. We will return to the MDX Sample Application, and perform the following steps:

1.  Clear the top area (the Query pane) of any queries or remnants that might appear.

2.  Ensure that FoodMart 2000 is selected as the database name in the DB box of the toolbar.

3.  Select the Warehouse cube in the Cube drop-down list box.

4.  Type the following query into the Query pane:

 -- MDX12-2:  Tutorial Query No. 2
 SELECT 
 {[Measures].[Warehouse Cost]
 } ON COLUMNS,
 { ORDER({[Warehouse].[USA].[WA].Children, [Warehouse].[USA].[OR].Children},  
 ([Measures].[Warehouse Cost]) , BDESC)}  ON ROWS
 FROM Warehouse
 WHERE ([Time].[Year].[1998])

Again, notice that the query we have input is identical to the last, with the exception of its use of the nonhierarchical BDESC sort type.

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

Analysis Services populates the Results pane, presenting the dataset shown in Illustration 2.


Illustration 2: Result Dataset - Order() Expression in Place, Nonhierarchized, Descending

We see the total Warehouse Cost for the specified states returned, once more, for year 1998. The Order() function sorts our Washington and Oregon warehouse city locations from highest to lowest, in terms of total Warehouse Cost, but this time the hierarchical level groupings are completely ignored. The warehouses cities are sorted solely with respect to Warehouse Cost. The Warehouse dimension's hierarchy, for purposes of our query, is "broken."

6.  Save the query as desired.

In conclusion, we can see that the Order() function provides flexible ways to arrange the display of our data, whether we wish to take hierarchies into consideration in our presentation or not.

Summary...

In this lesson, we explored the humble, yet versatile, Order() function. Order() finds itself an actor in many queries - queries that rank from the simplest to the most advanced. The Order() function provides the sorting capabilities we need within MDX, and is thus an important part of our analysis toolsets.

Through its support of hierarchies, MDX complicates sorting to a degree, as two general types of order, hierarchized and nonhierarchized, are possible. We explored how the Order() function stands ready to handle these scenarios, allowing us the options of arranging members within a given hierarchy, or of ignoring hierarchy completely in the arrangement of all members within the set we specify, and treating all to a simple sort. In addition to discussing the purpose of the Order() function, we examined of the syntax surrounding its use, and illustrated its application in practice exercises. Finally, we discussed the results we obtained in each exercise, remarking on the 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