Why SUM(null) is not 0 in Oracle?

前端 未结 6 1938
轻奢々
轻奢々 2021-01-04 08:33

It would be appreciated explaining the internal functionality of SUM function in Oracle, when encountering null values:
The result of

select sum(null)          


        
相关标签:
6条回答
  • 2021-01-04 08:40

    If you are looking for a rationale for this behaviour, then it is to be found in the ANSI SQL standards which dictate that aggregate operators ignore NULL values.

    If you wanted to override that behaviour then you're free to:

    Sum(Coalesce(<expression>,0))
    

    ... although it would make more sense with Sum() to ...

    Coalesce(Sum(<expression>),0)
    

    You might more meaningfully:

    Avg(Coalesce(<expression>,0))
    

    ... or ...

    Min(Coalesce(<expression,0))
    

    Other ANSI aggregation quirks:

    1. Count() never returns null (or negative, of course)
    2. Selecting only aggregation functions without a Group By will always return a single row, even if there is no data from which to select.

    So ...

    Coalesce(Count(<expression>),0)
    

    ... is a waste of a good coalesce.

    0 讨论(0)
  • 2021-01-04 08:41

    You're looking at this the wrong way around. SUM() operates on a column, and ignores nulls.

    To quote from the documentation:

    This function takes as an argument any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. The function returns the same data type as the numeric data type of the argument.

    A NULL has no data-type, and so your first example must return null; as a NULL is not numeric.

    Your second example sums the numeric values in the column. The sum of 0 + null + 1 + 2 is 3; the NULL simply means that a number does not exist here.

    Your third example is not an operation on a column; remove the SUM() and the answer will be the same as nothingness + 1 is still nothingness. You can't cast a NULL to an empty number as you can with a string as there's no such thing as an empty number. It either exists or it doesn't.

    0 讨论(0)
  • 2021-01-04 08:43

    This is incorrect: The sum of 0 + null + 1 + 2 is 3; select 0 + null + 1 + 2 total from dual;

    Result is null! Similar statements give result null if any operand is null.

    0 讨论(0)
  • 2021-01-04 08:46

    As Bohemian has pointed out, both SUM and AVG exclude entries with NULL in them. Those entries do not go into the aggregate. If AVG treated NULL entries as zero, it would bias the result towards zero.

    It may appear to the casual observer as though SUM is treating NULL entries as zero. It's really excluding them. If all the entries are excluded, the result is no value at all, which is NULL. Your example illustrates this.

    0 讨论(0)
  • 2021-01-04 08:49

    Arithmetic aggregate functions ignore nulls.

    • SUM() ignores them
    • AVG() calculates the average as if the null rows didn't exist (nulls don't count in the total or the divisor)
    0 讨论(0)
  • 2021-01-04 09:03

    SQL does not treat NULL values as zeros when calculating SUM, it ignores them:

    Returns the sum of all the values, or only the DISTINCT values, in the expression. Null values are ignored.

    This makes a difference only in one case - when the sequence being totalled up does not contain numeric items, only NULLs: if at least one number is present, the result is going to be numeric.

    0 讨论(0)
提交回复
热议问题