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 Sep 13, 2004

MDX Essentials: Numeric Functions: Introduction to the AVG() Function - Page 2

By William Pearson



To reinforce our understanding of the basics we have covered so far, we will use the AVG() function in a manner that illustrates its operation. We will do so in illustrative scenarios that place AVG() within the context of meeting a business need.

To begin, we will construct a SELECT query with a clearly defined calculated member that contains the AVG() function, and then present the returned dataset, via the core query, in a way that meets the business needs of a group of hypothetical information consumers. The intent is, of course, to demonstrate the operation of the AVG() function in a straightforward manner.

Let's return to the MDX Sample Application as a platform from which to construct and execute the MDX we examine, and to view the results 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.

Let's assume, for our practice example, that we have received a call from the Finance department of the FoodMart organization, requesting some information surrounding Warehouse Sales for a given time frame upon which they plan to perform some basic analysis. The Finance information consumers specifically wish to know the average total Warehouse Sales figure by Store Type, for the entire FoodMart organization, and for the time periods included in the current Warehouse cube. In addition, the consumers would like to see the average totals broken out by Product Family, ideally "across the x-axis," as they describe it, to make the performance of the various family types readily obvious in relation to each other.

We will meet the business need expressed by the Finance group by composing a query using a calculated member that contains the AVG() function. We will then present the returned data with a basic query that defines the presentation as requested, with the requested averages appearing in the appropriate arrangement.

5.  Type the following query into the Query pane:

-- MDX023-1 Basic Use of AVG() Function
   [Product].[AverageSales] AS 'Avg({[Product].[Product Family].Members}, 
  [Measures].[Warehouse Sales])'
   { [Product].[Product Family].Members, [Product].[AverageSales]} ON COLUMNS,
   NON EMPTY { [Store Type].Members } on rows
    ([Measures].[Warehouse Sales])  

6.  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 1 appears.

Illustration 1: Result Dataset - Basic Use of the AVG() Function

The above presentation meets the Finance group's requirements, permitting them to analyze average Warehouse Sales by Product Family component, for each of the Store Type groups. In addition, the column to the far right also allows them to see a simple combined average for all Products. (We will examine the generation of weighted averages in a subsequent article).

7.  Select File ` Save As, name the file MDX023-1, and place it in a meaningful location.

8.  Leave the query open for the next section.

We provide the results we have assembled to the information consumers, who are delighted with the presentation. The following day, we receive a call asking for more information in a somewhat similar, but a little more sophisticated vein. This time, the Finance group wants us to generate averages for two additional measures, Unit Shipped and Warehouse Profits, with the same row axis of Store Type groups. But they throw us another complication with their next stipulation: They want the total averages for products that number among the top ten individual product performers.

We are amused, once again, at the way that our consumers rapidly refine their requirements from the most basic requirements to gradually more elegant analysis. Seeing what they can have only leads to more in-depth requirements. The beautiful thing is that, the more we have met such needs, the more we can offer from the very start, and offer solutions within a preemptive, pilot development that gets our audiences where they need to be far faster than they could have imagined. Everyone benefits with well-founded business intelligence!

We take the following actions to meet the new consumer request:

9.  Select File ` New to open a blank query:

10.  Type the following query into the Query pane:

-- MDX023-2 More Sophisticated Example of Avg() Function in Use
   'TopCount ([Product].[Product Name].Members, 10, [Measures].[Warehouse Sales])'
    [Product].[OverLeadingAvg] AS 'Avg ([Leading10Products])'
   { [Measures].[Warehouse Sales], [Measures].[Units Shipped], 
   [Measures].[Warehouse Profit] } ON COLUMNS,
   NON EMPTY { [Store Type].Members } ON ROWS

11.  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 2 appears.

Illustration2: Result Dataset - More Sophisticated Use of the AVG() Function

The above presentation meets the Finance group's requirements, permitting them to perform the analysis they have envisioned. In effect, the results dataset is comprised of three measures that are broken down by six Store Types (including the All Store Types level). Each of the measures is averaged for each of the ten highest performing Products within the context of Warehouse Sales.

Another nuance in our solution is the absence of the numeric expression component in the AVG() function within our second calculated member. The function interprets this to mean that we are specifying the current members of the dimension and measure that belong to the set we have created just above. (Creation of a set whose purpose is to act as a proxy within a subsequent calculation, or other manipulation, in such a manner is an excellent way to achieve many such ends.)

Finally, we employ the calculated member as the slicer via the WHERE clause. We thus generate averages for each measure across each of the ten leading products, coming to an "aggregate average," as it were, as shown in Illustration 2 above.

12.  Select File ` Save As, name the file MDX023-2, and place it in a meaningful location.

13.  Select File -` Exit when ready to close the Sample Application.

Summary ...

In this lesson, we explored the MDX AVG() function. We commented generally upon the purpose of the function, to return the average value of a numeric expression as assessed over a specified set, and overviewed its operation. We examined the syntax employed in using AVG(), and then undertook illustrative practice examples based upon providing solutions for the expressed business needs of a hypothetical group of information consumers. Finally, we concluded each illustrative scenario with a brief discussion of the results datasets we obtained.

» 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