Re: Question about SUM and Nulls
From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 02/23/05
- Next message: Don Miller: "SP with Optional Parameter?"
- Previous message: Kalen Delaney: "Re: SQL Server system date?"
- In reply to: Michael C#: "Re: Question about SUM and Nulls"
- Next in thread: Michael C#: "Re: Question about SUM and Nulls"
- Reply: Michael C#: "Re: Question about SUM and Nulls"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 23 Feb 2005 23:41:05 +0100
On Wed, 23 Feb 2005 10:46:30 -0500, Michael C# wrote:
(snip)
>I find it strange that the geniuses who come up with this stuff decided that
>when performing a scalar operation with a NULL value the NULL cannot be
>treated as zero;
Hi Michael,
That's logical, because NULL is meant to be a marker for missing data.
Why the data is missing (unknown, not applicable, not relevant, ...) is
not important - to the database, the data is missing and the reason why
it's missing is not known or relevant.
Suppose I asked you what our age difference is, in other words: the
result of the calculation (your age) - (my age). For you, my age is not
known - it's NULL. Would you treat is as zero, subtract zero from your
age and post that as our age difference?
Oh, and would you also want NULL to be treated as zero in scalar
expressions involving multiplication? Or division?
> but when performing it in an aggregate operation NULLs can
>be ignored, effectively treating them as zero.
This statement is not correct: ignoring NULLS is *NOT* effective
treating them as zero. It might look that way, since SUM {1, 2} is the
same as SUM {0, 1, 2}. But AVG {1, 2} is not the same as AVG {0, 1, 2},
COUNT {1, 2} is not the same as COUNT {0, 1, 2} and MIN {1, 2} is quite
different from MIN {0, 1, 2}.
Anyway, ignoring NULLs makes perfect sense in aggregates, since these
are *defined* as the sum (or average, number, minimum, or maximum) of
all non-missing values in the set.
Of course, the SQL committee might have chosen to define the result of
any aggregate including a NULL in it's set as NULL. I don't see any
advantage of such a decision. But I do see how that would render these
aggregate functions almost useless.
Best, Hugo
-- (Remove _NO_ and _SPAM_ to get my e-mail address)
- Next message: Don Miller: "SP with Optional Parameter?"
- Previous message: Kalen Delaney: "Re: SQL Server system date?"
- In reply to: Michael C#: "Re: Question about SUM and Nulls"
- Next in thread: Michael C#: "Re: Question about SUM and Nulls"
- Reply: Michael C#: "Re: Question about SUM and Nulls"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|