SQL Aggregate Functions

TIBCO ActiveSpaces supports the use of aggregate functions as output columns in queries.

The aggregate functions supported are listed in the following table:
Function Description
AVG(<column>) Computes the arithmetic mean of the non-NULL values in the column. The type of the result is always a Double, regardless of the data type of the underlying column. If the data type of the underlying column is String, the value is converted to a Double. If the data type of the underlying column is Opaque, the data is treated as a String and then converted to a Double.
COUNT(<column>) COUNT(*)
Computes the number of non-NULL values in the column, or the number of rows. The result is always of type Long.
Note: COUNT() is not supported.
MIN(<column>) The minimum non-NULL value in the column. The type of the result matches the type of the underlying column.
MAX(<column>) The maximum non-NULL value in the column. The type of the result matches the type of the underlying column.
SUM(<column>) Computes the sum of the non-NULL values in the column. The type of the result is Long if the data type of the underlying column is Long, otherwise it is Double. If the data type of the underlying column is String, the value is converted to a Double. If the data type of the underlying column is Opaque, the data is treated as a String and then converted to a Double.
Aggregate functions can only be applied to a single column and cannot be applied to expressions or functions. For example, the following functions are not valid:
AVG(col1 + col2) 
MIN(trim(col1)) 
Whereas the following function is valid:
AVG(col1)
If all of the underlying values are NULL or cannot be converted to the appropriate data type for the function, the value of the function is NULL.

Using Aggregate Functions with the WHERE Clause

If the query includes a WHERE clause, the aggregate functions are applied to the rows that match the clause. If no WHERE clause is included, the functions are applied to all the rows in the table.

Using Aggregate Functions with the GROUP BY Clause

ActiveSpaces supports the GROUP BY clause and results in the aggregate functions being applied to the rows that match unique combinations of the GROUP BY columns. For example, the following query returns a set of rows - one for each state, with each row containing two columns, the state, and the mean salary for employees in that state:
SELECT state, AVG(salary) FROM employees GROUP BY state
If the query includes a GROUP BY clause, all the columns in the GROUP BY clause must be included as output columns. For instance, the following query is not valid because state is not one of the output columns:
SELECT AVG(salary) FROM employees GROUP BY state
Note: The GROUP BY clause can only be applied to a single column and cannot be applied to expressions or functions. The following code snippet is an invalid usage of the GROUP BY clause:
SELECT date(dtm) AS Year,COUNT(dtm) FROM t1 GROUP BY Year 

What Happens when the Query Contains Both, the WHERE and the GROUP BY Clause

If the query contains both a WHERE clause and a GROUP BY clause, the WHERE clause must precede the GROUP BY clause.

As is the case with other TIBCO ActiveSpaces queries, the order of the rows in the result set is not defined and the ORDER BY clause is currently not supported.

On large data sets, calculating the aggregate results can take a long time because large numbers of rows need to be processed before the computation can be completed. In such cases, when creating or executing the statement, it is advisable to set the TIBDG_STATEMENT_PROPERTY_DOUBLE_FETCH_TIMEOUT property accordingly.

The supported date-time functions can be used as output columns in the WHERE clause but not in the GROUP BY clause. Applications that send a large number of queries can choose a proxy binding strategy to distribute the queries across the proxies available to you. For more information, see Methods to Select a Proxy for a Client

The Usage of Indexes When the Query Contains Aggregation
Indexes can be used for a column that is an argument to a calculated function, or to GROUP BY, if the column has a constraint within the WHERE clause. For example, the following queries use the last_name_idx:
SELECT last_name, COUNT(last_name) AS count_last_name FROM t1 WHERE last_name LIKE 'B%' GROUP BY last_name
SELECT COUNT(last_name) AS count_last_name FROM t1 WHERE last_name LIKE 'B%
'SELECT last_name FROM t1 WHERE last_name LIKE 'C%' GROUP BY last_name
But the same queries without the WHERE constraints perform a full table scan.

Limitations

The following clauses are not supported:
  • HAVING
  • ORDER BY
  • DISTINCT
  • ALL
The following keywords cannot be passed as an argument to the aggregate functions:
  • DISTINCT
  • ALL
For example, the following statement is invalid:
SELECT COUNT(DISTINCT city) FROM t1, 
SELECT COUNT(ALL city) FROM t1)
Related reference