sql - Why SUM(null) is not 0 in Oracle? -


it appreciated explaining internal functionality of sum function in oracle, when encountering null values:
result of

select sum(null) dual; null 

but when null value in sequence of values (like sum of null-able column), calculated value of null value 0

select sum(value) ( select case when mod(level , 2) = 0 null else level end value dual connect level <= 10 ) 25 

this more interesting when seeing result of

select (1 + null) dual null 

as operation null result null (except is null operator).

==========================
some update due comments:

create table odd_table select sum(null) some_name dual; 

will result:

create table odd_table (   some_name number ) 

why some_name column of type number?

sql not treat null values zeros when calculating sum, ignores them:

returns sum of values, or distinct values, in expression. null values ignored.

this makes difference in 1 case - when sequence being totalled not contain numeric items, nulls: if @ least 1 number present, result going numeric.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -