Aggregates and Nulls - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-04-03
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

Aggregates (with the exception of COUNT(*)) ignore nulls in all computations.

A UDT column value is null only when you explicitly place a NULL in a column, not when a UDT instance has an attribute that is set to null.

Ignoring nulls can cause apparent nontransitive anomalies. For example, if there are nulls in column A or column B (or both), the following expression is true unless the values for columns A and B are both null in the same rows. In those cases, the entire row is disregarded in the aggregation.

SUM(A) + SUM(B) <> SUM(A+B) 

That is, if and only if field A and field B are both null for every occurrence of a null in either field is the preceding inequality false.

For examples of this behavior, see "Example: Employees Returned as Nulls" and "Example: Counting Employees Not Yet Assigned to a Department" in COUNT Result Type and Attributes. The aggregates are behaving as expected, the results are not mathematically anomalous.

Workarounds for this apparent nontransitivity issue:
  • Always define your numeric columns as NOT NULL DEFAULT 0.
  • Use the ZEROIFNULL function within the aggregate function to convert any nulls to zeros for the computation, for example SUM(ZEROIFNULL(x) + ZEROIFNULL(y)), which produces the same result as SUM(ZEROIFNULL(x)) + SUM(ZEROIFNULL(y)).