Re: select null + 0 question

From: "listrec" <listrec(at)epecon(dot)de>
To: "Jean-Christian Imbeault" <jc(at)mega-bucks(dot)co(dot)jp>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: select null + 0 question
Date: 2003-07-14 06:11:59
Message-ID: PCEJJIGCHIJCOOOGFAFEAEOKCAAA.listrec@epecon.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

The

select null + 0

is not the same as the

select sum(a) from a

statement.

Something equivalent would be

select sum(a) where a in (select null as a union select 1 as a)

In other words: As far as I understand it, sum() sums up all non null
values. In statement you have only one value, which happens to be null which
in return adds up to null. In your other statement you have one non null
value and sum returns the sum of this one value which is 1.

Detlef

-----Ursprungliche Nachricht-----
Von: pgsql-general-owner(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org]Im Auftrag von Jean-Christian
Imbeault
Gesendet: Montag, 14. Juli 2003 07:42
An: pgsql-general(at)postgresql(dot)org
Betreff: [GENERAL] select null + 0 question

Why is it that "select null + 1" gives null but "select sum(a) from
table" where there are null entries returns an integer?

Shouldn't the sum() and "+" operators behave the same?

TAL=# select null + 0;
?column?
----------

(1 row)

TAL=# select * from a;
a
---

1
(3 rows)

TAL=# select sum(a) from a;
sum
-----
1
(1 row)

Thanks,

Jean-Christian Imbeault

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joe Conway 2003-07-14 06:12:35 Re: select null + 0 question
Previous Message Alvaro Herrera 2003-07-14 06:05:01 Re: Is SQL silly as an RDBMS<->app interface?