Oracle9i OLAP Services Developer's Guide to the Oracle OLAP API
Release 1 (9.0.1)

Part Number A88756-01
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table of Contents
Contents
Go To Index
Index

Master Index

Feedback

Go to previous page Go to beginning of chapter Go to next page

Selecting Data, 4 of 5


Selecting Elements Based on Rank

What is ranking?

When a Source is sorted according to some attribute (or attributes), then the position of the elements of the Source represents a kind of ranking -- the so-called unique ranking. Finding the position of an element is discussed in "Finding the position of elements".

There are many other types of rankings that are not unique and that are called variant rankings. These are discussed in "Ranking elements in different ways".

Finding the position of elements

The position method returns a Source that represents the position of any given element in the original Source. The new Source has the type of Integer and has the original Source as an input. The position method returns a Source that represents the position of any given element of the base Source. If the base Source is sorted according to some attribute (or attributes), then the position represents a kind of ranking - the so called unique ranking.

You can also use the shortcut methods described in the following table to find elements based on their position in a Source object or to find the position of elements with the specified value or values.

Method 

Description 

at(pos) 

Identifies the values of elements in a Source that have the specified position. There are two versions of this method. One version allows you specify the position using a Source object; in the other, you specify position using an int value. 

first() 

Identifies the element or elements in a Source that have position 1. 

last() 

Identifies the element or elements in a Source that have the largest position value. 

positionOfValue(value) 

Identifies the positions of elements in a Source that have the specified value. There are two versions of this method. One version allows you specify the value using a Source; in the other, you specify value using a String

positionOfValues(values) 

Identifies the positions of elements in a Source that have the specified values. There are two versions of this method. One version allows you specify the value using a Source; in the other, you specify value using an array of String objects. 

Example: Finding the positions of elements when there are no keys

Assume that there is a Source named products whose elements are the unique identifiers of products. To create a new Source whose elements are the positions of the elements of products, issue the following code.

Source productsPosition = products.position();

A tabular representation of productsPosition showing the position of the elements in products is shown below. Note that the position() method is one based.

Input 

Element 

products 

Integer 

395 

400 

405 

415 

420 

425 

... 

 

Example: Finding the positions of elements when there are inputs

Assume that there is a Source named unitsSoldByCountry (shown below) that has an input of products, an output of countries, and elements whose values are the total number of units for each product sold for each country.

Input 

Output 

Element 

Position 

product 

countries 

Integer 

 

395 

Australia 

500 

395 

United States 

800 

... 

... 

 

... 

49780 

Australia 

10000 

49780 

United States  

50 

49780 

... 

 

... 

To create a new Source named positionUnitsSoldByCountry whose elements are the positions of the elements of unitsSoldByCountry, issue the following code.

Source positionUnitsSoldByCountry = unitsSoldByCountry.position();

Ranking elements in different ways

The following table provides example code for ranking elements in different ways where the Source (named base) whose elements you want to rank has two inputs named input1 and input2.

Rank 

Example Code 

ascending 

Source sortedTuples = base.sortAscending()
 

descending 

Source sortedTuples = base.sortDescending()
 

same order as another Source 

Source sortedTuples = base.sortAscending
   (Source sortValue)
 

reverse order as another Source 

Source sortedTuples = base.sortDescending
   (Source sortValue)
 

minimum 

Source sortedTuples = base.join(input1).sortDescending(input2);
Source equivalentRankedTuples = 
  sortedTuples.join(input2, input2);
NumberSource minRank = sortedTuples.
  positionOfValues(equivalentRankedTuples).minimum();

The minimum ranking differs from unique ranking in the way it deals with ties (elements in the Source that share the same value for the attribute). All ties are given the same rank, which is the minimum possible.  

maximum 

Source sortedTuples = base.join(input1).sortDescending(input2);
Source equivalentRankedTuples = 
   sortedTuples.join(input2, input2);
NumberSource maxRank = sortedTuples.positionOfValues
     (equivalentRankedTuples).maximum();

The maximum ranking differs from unique ranking in the way it deals with ties (elements in the Source that share the same value for the attribute). All ties are given the same rank, which is the maximum possible rank.  

average 

Source sortedTuples = base.join(input1).sortDescending(input2;
Source equivalentRankedTuples = 
  sortedTuples.join(input2, input2);
NumberSource averageRank = sortedTuples.positionOfValues
   (equivalentRankedTuples).average();

The average ranking differs from unique ranking in the way it deals with ties (elements in the Source that share the same value for the attribute). All ties are given the same rank, which is equal to the average unique rank for the tied values. 

packed 

Source tuples = base.join(o
   utput1);
Source firstEquivalentTuple = tuples.join(input2, input2.first();
Source packedRank = firstEquivalentTuple.join(tuples).
  sortDescending(input2).positionOfValues(base.value().
  join(time.value());

Packed ranking, also called dense ranking, is distinguished from minimum ranking by the fact that the ranks are packed into consecutive integers. 

percentile 

Source sortedTuples = base.join(input1).sortDescending(input2);
Source equivalentRankedTuples = 
  sortedTuples.join(input2, input2);
NumberSource minRank = sortedTuples.
     positionOfValues(equivalentRankedTuples).minimum();
NumberSource percentile = minRank.minus(1).times(100).
     div(sortedTuples.count());

This code uses the following formula to calculate the percentile of an attribute A for a Source S with N elements.

Percentile(x) =  number of elements 
(for which the A differs from A(x)) 
that come before x in the ordering * 100 / N

The percentile, then, is equivalent to the minimum rank -1 * 100 / N.  

ntile 

NumberSource n = ...;
Source sortedTuples = base.join(input1).sortDescending(input2);
NumberSource uniqueRank = sortedTuple.
      positionOfValues(base.value().join(input1.value());
NumberSource ntile = uniqueRank.times(n).
     div(sortedTuples.count()).ceiling();

In this code, the ntile ranking for a given n is defined by dividing the ordered Source of size count into n buckets, where the bucket with rank k is of size. The ntile rank is equivalent to the following formula.

ceiling*((uniqueRank*n)/count)


Go to previous page Go to beginning of chapter Go to next page
Oracle
Copyright © 1996-2001, Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table of Contents
Contents
Go To Index
Index

Master Index

Feedback