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 21, 2008

Combine BottomCount() with Other MDX Functions to Add Sophistication - Page 2

By William Pearson

As we might have expected, this simple beginning hardly achieves our ultimate objectives: it returns the bottom four Cities from the perspective of total Reseller Sales Amount for CY 2003 – but the bottom four from the entire Adventure Works organization. (The bottom four performers happen to have had no Reseller Sales – therefore the “nulls” we see in the Reseller Sales Amount column.) We will next advance beyond the initial, basic query above, where we employ another MDX function, Descendants(), to supply the «Set» argument for the BottomCount() function; we have a bit more to do to reach our goal of presenting the bottom performer Cities by U.S. State.

NOTE: For information surrounding the Descendants() function, see my article MDX Member Functions: The "Family" Functions, a member of the MDX Essentials series at Database Journal. For an introduction to the .CurrentMember function, see MDX Member Functions: "Relative" Member Functions, another article in the same monthly column.

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

4.  Leave the query open for the next section.

Next, we need to add logic to generate the same “bottom four” data, but for an ancestor of City (another level in the Geography hierarchy of the Geography dimension), the State that the City inhabits. To do so, we will introduce more MDX functions, as we see in the next step.

5.  Within the query we have saved as MDX072-1-1, replace the top comment lines of the query with the following:

-- MDX072-1-2, BottomCount(), combined with Ancestor()  
--   and .Name in a calculated member, to provide  
--   U. S. States with each of "Bottom 4 Least Performing    
--   (with regard to Total Reseller Sales Amounts) Cities 
--   in Adventure Works Organization in CY 2003"

6.  Save the query as MDX072-1-2, to keep MDX072-1-1 intact as a working sample.

7.  Add the following lines to the query, between the top comment line we just replaced, and the SELECT keyword that begins the query:

WITH 
MEMBER
   [Measures].[State] 
AS
   'ANCESTOR([Geography].[Geography].CURRENTMEMBER,     
      [Geography].[Geography].[State-Province]).NAME'

This will create the calculated member State, which will allow us to present the States alongside their respective member Cities.

NOTE: For an introduction to the Ancestor() function, see my article MDX Member Functions: The "Family" Functions, another member of the MDX Essentials series at Database Journal.

8.  Modify the ON AXIS(0) line of the query, which already contains the Reseller Sales Amount measure, to contain the new State calculated member we defined above, as follows:

{[Measures].[State], [Measures].[Reseller Sales Amount]} ON AXIS(0),

9.  Leave the remainder of the query in its original state.

The Query pane appears as depicted in Illustration 3, with our modifications marked.


Illustration 3: The Query with Added Calculated Member and Axis(0) Specification Modification

10.  Execute the query by clicking the Execute (!) button in the toolbar.

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


Illustration 4: Results Dataset – After Our Query Modifications

We note that the State to which each of the “bottom four” Cities (still the bottom four for the entire Adventure Works organization, we also note) belongs appears in the results. Our calculated member has taken us a step closer to realization of the information consumers’ requirements.

11.  Re-save the file as MDX072-1-2.

12.  Leave the query open for the next step.

At this point, all that remains is to generate the “bottom four” data for each of the U.S. States, limiting the States to the U.S. States within which Adventure Works reseller sales occur. The word “generate” is particularly apt here: we will use the Generate() function in our efforts to return the “bottom four” from each of the U.S. States involved.

13.  Within the query we have saved as MDX072-1-2, replace the top comment line of the query with the following:

-- MDX072-1-3, Addition of Generate() to retrieve 
--   4 Worst Performing Cities in Each U. S. State (with regard to 
--      total Reseller Sales Amounts) in CY 2003"

14.  Save the query as MDX072-1-3, to keep MDX072-1-2 intact as a working sample.

15.  Substitute the following for the entire existing ON AXIS(1) specification within the SELECT statement of the query:

{GENERATE
    (
       [Geography].[Geography].[Country].[United States].CHILDREN,
           BOTTOMCOUNT(
              DESCENDANTS(
                 [Geography].[Geography].CURRENTMEMBER,
                     [Geography].[Geography].[City]   
                        ),
                     4,
                 [Measures].[Reseller Sales Amount])
              )
           }
        ON AXIS(1)

(In actuality, we are only adding the Generate() statement to the “front end” of the ON AXIS(1) specification, as it already existed from earlier steps, with a closing right parenthesis - “)” - just before the ON AXIS keyword).

The Generate() statement builds a set based upon the bottom performing Cities, as retrieved by the BottomCount() function (with Reseller Sales Amount as the <<Numeric Expression>> argument, and the Descendants() function, once again, specifying the «Set» argument as the individual Cities), limited to U.S. States (the “children” of the United States level of the Geography dimension / Geography hierarchy.

NOTE: For information surrounding the Generate() function, see my MDX Essentials article Set and String Functions: The GENERATE() Function, (where we perform a combination of Generate() and BottomCount(), as well). For an introduction to the .Children function, see MDX Member Functions: The "Family" Functions, within the same series.

16.  Leave the remainder of the query in its original state.

The Query pane appears as depicted in Illustration 5, with our additions marked.


Illustration 5: The Query with Substituted Comments and New ON AXIS(1) Specification

17.  Execute the query by clicking the Execute (!) button in the toolbar.

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


Illustration 6: Results Dataset (Partial View) – After Final Modifications

Finally, we see the presentation as requested by the information consumers: CY 2003 total Reseller Sales Amounts for the four Cities with the lowest Reseller Sales Amounts - for each of the U. S. States within which Adventure Works has conducted Reseller Sales operations. We note that several Cities display “nulls” – this is appropriate in our simple example where, for instance, two of the four least performing” Cities in the State of Alabama appear in our dataset because they had no Reseller Sales activity, over the Calendar Year by which we have sliced within our query. (There are other ways we can refine the query for presentation purposes – say to show the “least four” Cities with non-zero values, etc., - but such refinements are beyond the scope of this article.)

18.  Re-save the file as MDX072-1-3.

19.  Select File -> Exit to leave the SQL Server Management Studio, when ready.

Summary ...

This article served as an extension of our previous introductory article, Basic Set Functions: The BottomCount() Function. We briefly reviewed the operation of the BottomCount() function, and then extended our examination of BottomCount() to include a somewhat more sophisticated use.

We defined an illustrative business need, as posed to us by hypothetical groups of information consumers, and, after discussing the need in general, with regard to challenges inherent to meeting them with the BottomCount() function in particular. We then constructed, in a step-by-step manner, a final query to meet the expressed requirements using a combination of BottomCount() and other MDX functions we have explored in articles of the MDX Essentials series. Throughout our practice example, we discussed the syntax contained within the solutions we constructed for the information consumers, as well as the datasets we obtained in employing BottomCount() and other functions, together with other surrounding considerations.

About the MDX Essentials Series ...

This article is a member of the MDX Essentials series, a monthly column designed to provide hands-on application of the fundamentals of the Multidimensional Expressions (MDX) language, with each article progressively adding features designed to meet specific real-world needs.

For more information about the column in general, as well as the software and systems requirements for getting the most out of the lessons included, please see my first article, MDX at First Glance: Introduction to MDX Essentials, among others.

» 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