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 Mar 24, 2003

MDX Member Functions: The "Family" Functions - Page 5

By William Pearson

The Ancestor() Function

The Ancestor() function retrieves the member, at the specified level, or at the specified distance, that is the ancestor (the parent, parent of parent, or higher) of the source member under consideration. Like the .Parent and .Children functions, it travels within a "vertical" scope, moving up between hierarchical levels from the member to which it is applied.

A difference exists between the functions, however, in that the Ancestor() function can contain the aforementioned level, or distance, component, as we will discuss in the Syntax section below.

Syntax

The Ancestor() function is applied differently than the .Parent and .Children functions, in that it is not "appended" to the source member. The source member is placed within the parentheses to the right of the word Ancestor, as shown in the following illustration:

Ancestor(member, level)

Or

Ancestor(member, distance)

An illustration of the Ancestor() function (using the level target) follows:

SELECT
{(Measures.[Warehouse Sales]), (Measures.[Warehouse Cost])} ON COLUMNS,
{(Ancestor([Time].[Year].[1997].[Q2].[4], [Time].[Quarter]))} ON ROWS
FROM [Warehouse]

The result dataset returned would appear as shown in the illustration below:


Illustration 10: The Results of the Ancestor() Function in a Query

In the illustration above, a target level that equaled the level of the member within the function would have returned the member itself.

If a distance target is specified, the distance component is represented by a number. The number represents "number of hierarchical steps above the source member." The member itself is returned if the number is zero, reasonably enough.

Practice

Let's reinforce our understanding of how the Ancestor() function operates by using it in a couple of queries. We will use the second of the syntax structures (Ancestor(member, distance) in our first example.

We will return to the MDX Sample Application, and create a query to return the ancestor member two levels above the source member that we used in the Syntax illustration above. We will be specifying the Units Shipped measure as the slicer dimension in this example.

14.         Type the following query into the Query pane:

-- MDX05-5:  Tutorial Query No. 5
SELECT
{(Measures.[Warehouse Sales]), (Measures.[Warehouse Cost])} ON COLUMNS,
{(Ancestor([Time].[Year].[1997].[Q2].[4], 2))} ON ROWS
FROM [Warehouse]

15.         Click the Run button to execute the query.

We see the result dataset below, which appears in the Results pane as soon as Analysis Services fills the cells specified by the query.


Illustration 11: The Query Result Dataset

The query delivers the results that we requested: The target member, specified as "two steps above the source member," is Year (two steps above Month in the Time dimension hierarchy), together with the respective total Units Shipped.

Now let's illustrate the operation of the Ancestor() function once again, with another example.

16.         Type the following into the Query pane:

-- MDX05-6:  Tutorial Query No. 6
SELECT
{(Measures.[Units Shipped]), (Measures.[Units Ordered])} ON COLUMNS,
{(Ancestor([Store].[Store Country].[USA].[OR].[Portland], 9
 [Store].[Store Country]))} ON ROWS
FROM [Warehouse]

Here, we're specifying two unit measures in our columns, and don't, at least in this case, require the slicer specification (WHERE). We are also using the Ancestor() function, this time with the level target, versus the distance target we used in the first practice exercise. Because we specify the Store Country level, with Portland as the source member, we would expect to see USA in the result dataset. We will not be disappointed in our expectations.

17.         Click Query on the top menu, and then select Run.

The Results pane appears as shown below.


Illustration 12: The Query Results

The result dataset displays the USA member, along with the corresponding Units totals.

We see that we can obtain the targeted ancestor member with two different approaches. As will be the case with the .Parent and .Children functions that we have explored earlier, we will find the Ancestor() function to be a powerful tool later when we call upon it for use with relative members.

Next in Our Series ...

In this lesson, MDX Member Functions: The "Family" Functions, we continued the multiple-article Member Functions segment that focuses upon the member functions (and operators, as appropriate) one at a time, contrasting the uses and effects of each. Specifically, we explored the .Parent, .Children and Ancestor() functions, discussing the information they return, together with syntactical points surrounding their use. We illustrated further how to take advantage of these useful functions by performing practice exercises, and commenting on the result datasets we obtained. As a part of the lesson, we also introduced the WHERE clause, within the context of specifying a slicer dimension for some of our queries.

In our next lesson, MDX Member Functions: More "Family" Functions, we will expose the .FirstChild, .LastChild, .FirstSibling and .LastSibling member functions. We will discuss each function, illustrate the appropriate syntax required for its use, and test our understanding of how to take advantage of each of these useful functions by performing practice exercises, and commenting on the result datasets we obtain.

» 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