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

From: hubert depesz lubaczewski <depesz(at)depesz(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 10:06:13
Message-ID: 20070905100613.GA27754@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Sep 05, 2007 at 11:15:43AM +0200, Stefan Schwarzer wrote:
> SELECT DISTINCT (tpes_total.y_2004 / pop_total.y_2004) AS
> y_2004, countries_view.name AS name
> FROM pop_total, countries_view
> LEFT JOIN tpes_total ON tpes_total.id = countries_view.id
> WHERE pop_total.y_2004<> '0' AND pop_total.y_2004<> '-9999' AND
> tpes_total.y_2004 <> '-9999' AND countries_view.id = pop_total.id
> ORDER BY name ASC
> So, I guess I'd need some kind of IF statement to do the calculation
> only with "valuable" numbers and pass the others as they are.
> But I have no idea how this would work.

SELECT DISTINCT
(
CASE
WHEN pop_total.y_2004<> '-9999' AND tpes_total.y_2004 <> '-9999'
then tpes_total.y_2004 / pop_total.y_2004
ELSE '-9999'
END
) AS y_2004,
countries_view.name AS name

should work.

depesz

--
quicksil1er: "postgres is excellent, but like any DB it requires a
highly paid DBA. here's my CV!" :)
http://www.depesz.com/ - blog dla ciebie (i moje CV)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message blay bloo 2007-09-05 10:53:51 Re: How to 'register' functions, so they can be called (plpythonu)
Previous Message Stefan Schwarzer 2007-09-05 09:15:43 SQL query with IFs (?) to "Eliminate" NULL Values