Oracle® Business Intelligence Server Administration Guide > Oracle BI Server SQL Reference > SQL Reference >

Aggregate Functions


Aggregate functions perform work on multiple values to create summary results. The aggregate functions cannot be used to form nested aggregation in expressions on logical columns that have a default aggregation rule defined in the Aggregation tab of the Logical Column dialog box. To specify nested aggregation, you need to define a column with a default aggregation rule and then request the aggregation of the column in a SQL statement.

Avg

Calculates the average (mean) value of an expression in a result set. Has to take a numeric expression as its argument.

Syntax:

AVG (n_expression)

where:

n_expression

Any expression that evaluates to a numerical value.

AvgDistinct

Calculates the average (mean) of all distinct values of an expression. Has to take a numeric expression as its argument.

Syntax:

AVG (DISTINCT n_expression)

where:

n_expression

Any expression that evaluates to a numerical value.

BottomN

Ranks the lowest n values of the expression argument from 1 to n, 1 corresponding to the lowest numerical value. The BOTTOMN function operates on the values returned in the result set.

Syntax:

BOTTOMN (n_expression, n)

where:

n_expression

Any expression that evaluates to a numerical value.

n

Any positive integer. Represents the bottom number of rankings displayed in the result set, 1 being the lowest rank.

NOTE:  A query can contain only one BOTTOMN expression.

Count

Calculates the number of rows having a nonnull value for the expression. The expression is typically a column name, in which case the number of rows with nonnull values for that column is returned.

Syntax:

COUNT (expression)

where:

expression

Any expression.

CountDistinct

Adds distinct processing to the COUNT function.

Syntax:

COUNT (DISTINCT expression)

where:

expression

Any expression.

Count (*) (CountStar)

Counts the number of rows.

Syntax:

COUNT(*)

For example, if a table named Facts contained 200,000,000 rows, the following query would return the following results:

SELECT COUNT(*) FROM Facts

COUNT(*)

200000000

First

Selects the first returned value of the expression argument. The FIRST function is limited to defining dimension-specific aggregation rules in a repository. You cannot use it in SQL statements.

The FIRST function operates at the most detailed level specified in your explicitly defined dimension. For example, if you have a time dimension defined with hierarchy levels day, month, and year, the FIRST function returns the first day in each level.

You should not use the FIRST function as the first dimension-specific aggregate rule. It might cause queries to bring back large numbers of rows for processing in the Oracle BI Server causing poor performance.

Syntax:

FIRST (expression)

where:

expression

Any expression.

GroupByColumn

For use in setting up aggregate navigation. It specifies the logical columns that define the level of the aggregate data existing in a physical aggregate table.

For example, if an aggregate table contains data grouped by store and by month, specify the following syntax in the content filter (General tab of Logical Source dialog):

GROUPBYCOLUMN(STORE, MONTH)

The GROUPBYCOLUMN function is only for use in configuring a repository; you cannot use it to form SQL statements.

GroupByLevel

For use in setting up aggregate navigation. It specifies the dimension levels that define the level of the aggregate data existing in a physical aggregate table.

For example, if an aggregate table contains data at the store and month levels, and if you have defined dimensions (Geography and Customers) containing these levels, specify the following syntax in the content filter (General tab of Logical Source dialog):

GROUPBYLEVEL (GEOGRAPHY.STORE, CUSTOMERS.MONTH)

The GROUPBYLEVEL function is only for use in configuring a repository; you cannot use it to form SQL statements.

Last

Selects the last returned value of the expression. The LAST function is limited to defining dimension-specific aggregation rules in a repository. You cannot use it in SQL statements.

The LAST function operates at the most detailed level specified in your explicitly defined dimension. For example, if you have a time dimension defined with hierarchy levels day, month, and year, the LAST function returns the last day in each level.

You should not use the LAST function as the first dimension-specific aggregate rule. It might cause queries to bring back large numbers of rows for processing in the Oracle BI Server causing poor performance.

Syntax:

LAST (expression)

where:

expression

Any expression.

Max

Calculates the maximum value (highest numeric value) of the rows satisfying the numeric expression argument.

Syntax:

MAX (expression)

where:

expression

Any expression.

The MAX function resets its values for each group in the query, according to the rules outlined in Display Function Reset Behavior.

Median

Calculates the median (middle) value of the rows satisfying the numeric expression argument. When there are an even number of rows, the median is the mean of the two middle rows. This function always returns a double.

Syntax:

MEDIAN (n_expression)

where:

n_expression

Any expression that evaluates to a numerical value.

The MEDIAN function resets its values for each group in the query, according to the rules outlined in Display Function Reset Behavior.

Min

Calculates the minimum value (lowest numeric value) of the rows satisfying the numeric expression argument.

Syntax:

MIN (expression)

where:

expression

Any expression.

The MIN function resets its values for each group in the query, according to the rules outlined in Display Function Reset Behavior.

NTile

The NTILE function determines the rank of a value in terms of a user-specified range. It returns integers to represent any range of ranks. In other words, the resulting sorted data set is broken into a number of tiles where there are roughly an equal number of values in each tile.

Syntax:

NTILE (n_expression, n)

where:

n_expression

Any expression that evaluates to a numerical value.

n

A positive, nonnull integer that represents the number of tiles.

If the n_expression argument is not null, the function returns an integer that represents a rank within the requested range.

NTile with n=100 returns what is commonly called the percentile (with numbers ranging from 1 to 100, with 100 representing the high end of the sort). This value is different from the results of the Oracle BI Server percentile function, that conforms to what is called percent rank in SQL 92 and returns values from 0 to 1.

Percentile

Calculates a percent rank for each value satisfying the numeric expression argument. The percent rank ranges are from 0 (1st percentile) to 1 (100th percentile), inclusive.

The PERCENTILE function calculates the percentile based on the values in the result set of the query.

Syntax:

PERCENTILE (n_expression)

where:

n_expression

Any expression that evaluates to a numerical value.

The PERCENTILE function resets its values for each group in the query according to the rules outlined in Display Function Reset Behavior.

PeriodAgo

A time series aggregation function for relational data sources only. Calculates the aggregated value from the current time back to a specified time period. For example, PeriodAgo can produce sales for every month of the current quarter and the corresponding quarter-ago sales.

If unsupported metrics are requested, NULL values will be returned and a warning entry will be written to the NQQuery.log file when the logging level equals three or above. Multiple PeriodAgo functions can be nested if all the PeriodAgo functions have the same level argument.

You can nest exactly one PeriodToDate and multiple PeriodAgo functions if they each have the same level argument.

Syntax:

PeriodAgo(<time-level>, <offset>, <measure>)

PeriodToDate

A time series aggregation function for relational data sources only. PeriodToDate aggregates a measure attribute from the beginning of a specified time period to the currently displayed time. For example, this function can calculate Year to Date sales.

If unsupported metrics are requested, NULL values will be returned and a warning entry will be written to the NQQuery.log file when the logging level equals three or above. A PeriodToDate function may not be nested within another PeriodToDate function.

You can nest exactly one PeriodToDate and multiple PeriodAgo functions if they each have the same level argument.

Syntax:

PeriodToDate(<time-level>, <measure>)

Rank

Calculates the rank for each value satisfying the numeric expression argument. The highest number is assigned a rank of 1, and each successive rank is assigned the next consecutive integer (2, 3, 4,...). If certain values are equal, they are assigned the same rank (for example, 1, 1, 1, 4, 5, 5, 7...).

The RANK function calculates the rank based on the values in the result set of the query.

Syntax:

RANK (n_expression)

where:

n_expression

Any expression that evaluates to a numerical value.

The RANK function resets its values for each group in the query according to the rules outlined in Display Function Reset Behavior.

Calculating Absolute Calendar Fields

To calculate absolute calendar fields such as abs_month, you use the prebuilt Rank operator.

Syntax:

Rank(<ordering key>, <partitioning key>, <at_distinct key>)

Where:

<ordering key>

Key that is used to order the input for the purpose of assigning rank.

<partitioning key>

Key that is used to partition data. Ranking is restarted from zero at the beginning of every partition.

<at_distinct key>

Dictates that ranking is increased only across rows with differing values in this key. The rows with the same value for this key have the same rank.

The following is a list of examples:

  • Rank(<chronological key>, null, <year key columns>) returns abs_year.
  • Rank(<chronological key>, null, <month key columns>) returns abs_month.
  • Rank(<chronological key>, <year key columns>, <month key columns>) returns month_in_year.

StdDev

The STDDEV function returns the standard deviation for a set of values. The return type is always a double.

Syntax:

STDDEV([ALL | DISTINCT] n_expression)

where:

n_expression

Any expression that evaluates to a numerical value.

  • If ALL is specified, the standard deviation is calculated for all data in the set.
  • If DISTINCT is specified, all duplicates are ignored in the calculation.
  • If nothing is specified (the default), all data is considered.

There are two other functions that are related to STDDEV:

STDDEV_POP([ALL | DISTINCT] n_expression)

STDDEV_SAMP([ALL | DISTINCT] n_expression)

STDDEV and STDDEV_SAMP are synonyms.

The STDDEV function resets its values for each group in the query according to the rules outlined in Display Function Reset Behavior.

Sum

Calculates the sum obtained by adding up all values satisfying the numeric expression argument.

Syntax:

SUM (n_expression)

where:

n_expression

Any expression that evaluates to a numerical value.

The SUM function resets its values for each group in the query according to the rules outlined in Display Function Reset Behavior.

SumDistinct

Calculates the sum obtained by adding all of the distinct values satisfying the numeric expression argument.

Syntax:

SUM(DISTINCT n_expression)

where:

n_expression

Any expression that evaluates to a numerical value.

TopN

Ranks the highest n values of the expression argument from 1 to n, 1 corresponding to the highest numerical value.

The TOPN function operates on the values returned in the result set.

Syntax:

TOPN (n_expression, n)

where:

n_expression

Any expression that evaluates to a numerical value.

n

Any positive integer. Represents the top number of rankings displayed in the result set, 1 being the highest rank.

A query can contain only one TOPN expression.

The TOPN function resets its values for each group in the query according to the rules outlined in Display Function Reset Behavior.

Oracle® Business Intelligence Server Administration Guide Copyright © 2007, Oracle. All rights reserved.