Function expressions allow values within columns to be calculated based on values within other columns on a row-by-row basis. But in many cases, you may want to calculate a single value across the values of multiple rows in one or more columns. You can use aggregate expressions for this purpose.
The following table lists a selection of commonly-used aggregate functions supported by SAP HANA. For other, less-commonly used aggregate functions, please refer to SAP HANA documentation.
Aggregate Expressions Supported by SAP HANA
| Aggregate Name | Description |
|---|---|
| COUNT | Returns the number of rows. |
| MIN, MAX | Returns the minimum or maximum value of an input column with a numeric data type. |
| SUM, AVG | Returns the sum or the arithmetic mean value of an input column with a numeric data type. |
COUNT(*) Expression
You can calculate the number of rows in the result set using COUNT(*). Rows containing only NULL values are included.
What is the quantity of cars with the brand Audi?
123SELECT COUNT(*)
FROM Car
WHERE Brand = 'Audi';| COUNT(*) |
|---|
| 4 |
COUNT(<column>) Expressions
You can calculate the number of values within a single column. NULL values are not included.
You can only use a single column as parameter of COUNT().
- How many cars are registered to an owner?
- This does not calculate the number of different owners!
12SELECT COUNT(Owner)
FROM Car;| COUNT(OWNER) |
|---|
| 18 |
COUNT(DISTINCT <column>) Expression
You can calculate the number of distinct, non-NULL values of a certain column. You can use only a single column as parameter for COUNT (DISTINCT). NULL values are not included.
How many different owners have a registered car?
12SELECT COUNT(DISTINCT Owner)
FROM Car;| COUNT(DISTINCT OWNER) |
|---|
| 8 |
Differences between the COUNT Expressions

The DISTINCT modifier may also be used with the SUM or AVERAGE aggregates, but not with the MAX or MIN aggregates.
MIN and MAX Expressions
You can calculate the minimum or maximum value in a column. What is the horsepower range of the registered cars?
12SELECT MIN(HP), MAX(HP)
FROM Car;| MIN(HP) | MAX(HP) |
|---|---|
| 75 | 260 |
Combining Aggregate and Function Expressions
You can combine aggregate expressions and normal functions. In which year was the youngest owner born?
12SELECT MAX(YEAR(Birthday)) AS Year
FROM Owner; 12SELECT YEAR(MAX(Birthday)) AS Year
FROM Owner; | YEAR |
|---|
| 1986 |
Note that in general, the order in which functions and aggregate expressions are nested matters. As an example, the following two SELECT statements lead to different results:
12SELECT ABS( MAX(0-HP) ) FROM Car; -- Result: 75
SELECT MAX( ABS(0-HP) ) FROM Car; -- Result: 260Aggregate Expressions
The WHERE clause may be included with aggregate functions. Aggregates are evaluated after the WHERE condition.
What is the horsepower range of BMWs?
123SELECT MIN(HP), MAX(HP)
FROM Car
WHERE Brand = 'BMW';| MIN(HP) | MAX(HP) |
|---|---|
| 140 | 184 |
SUM(<column>) and AVG(<column>) Expressions
You can add up the values in a column or determine their arithmetic mean. The WHERE clause is evaluated before aggregation.
Individual NULL values contained in the column do not result in NULL for the sum or average (as long as there is at least one non-NULL value).
What is the sum and the mean of horsepower for all Audis?
123SELECT SUM(HP), AVG(HP)
FROM Car
WHERE Brand = 'Audi';| SUM(HP) | AVG(HP) |
|---|---|
| 744 | 186 |