Re: If statements on select

From: Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl>
To: "Alejandro D(dot) Burne" <alejandro(dot)dburne(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: If statements on select
Date: 2005-04-15 12:48:41
Message-ID: 20050415124841.GB31521@dcc.uchile.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Apr 15, 2005 at 09:13:40AM -0300, Alejandro D. Burne wrote:
> Hi, I'm translating an app made with visual foxpro and mysql as rdbms
> to postgres.
> I've got many querys with this sintax:
>
> select code1, sum(if(cond1, price,0)), sum(if(cond2,price,0)) from ...

Use CASE WHEN cond1 THEN price ELSE 0 END.

But note that if your condition is to test price for NULL, it's easier to do

COALESCE(price, 0)

--
Alvaro Herrera (<alvherre[(at)]dcc(dot)uchile(dot)cl>)
"El día que dejes de cambiar dejarás de vivir"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2005-04-15 13:41:41 Re: SQL Question
Previous Message GIROIRE, Nicolas (COFRAMI) 2005-04-15 12:23:26 Re: SQL Question