Re: SQL query with IFs (?) to "Eliminate" NULL Values

From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: "Stefan Schwarzer" <stefan(dot)schwarzer(at)grid(dot)unep(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL query with IFs (?) to "Eliminate" NULL Values
Date: 2007-09-05 14:47:35
Message-ID: dcc563d10709050747s39c78fccp8dca67a049b3ac3b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 9/5/07, Stefan Schwarzer <stefan(dot)schwarzer(at)grid(dot)unep(dot)ch> wrote:
> Hi there,
>
> I want to calculate per Capita values on-the-fly, taking for example
> the "Total GDP" data set and divide it by "Total Population". Now,
> each of these data sets have a couple of "0" or "-9999" values (the
> latter being the indicator for : "no data available").

Why not use real NULLs there. If you do avg over a set of data with
nulls, the nulls don't count. i.e.:

create table test (a int);
insert into test values (1);
insert into test values (2);
insert into test values (NULL);
select avg (a) from test;
avg
--------------------
1.5000000000000000

select count (a) from test;
count
-------
2

NULL acts that way automatically. You're jumping through hoops to
implement what has already been implemented.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2007-09-05 14:55:01 Re: SQL for Deleting all duplicate entries
Previous Message Rodrigo De León 2007-09-05 14:37:18 Re: Partition Reindexing