Re: no IF - am I missing something ? - Mailing list pgsql-general

From Thomas F.O'Connell
Subject Re: no IF - am I missing something ?
Date
Msg-id 99c1889effb3a156f93515ed3435fa54@sitening.com
Whole thread Raw
In response to no IF - am I missing something ?  ("Richard Crawley" <richard.crawley@quadronservices.co.uk>)
Responses Re: no IF - am I missing something ?  (Michael Fuhr <mike@fuhr.org>)
List pgsql-general
The number of lines depends merely on where you place your line breaks.

IF(days_complete <= 120, job_price, 0)AS Days_120

could be written as:

CASE WHEN days_complete <= 120 THEN job_price ELSE 0 END AS Days_120

There might be somewhat less syntactic sugar, but this is not a five
line expression and, to me, is more readable than a comma-delimited
list where position alone indicates function in the expression.

-tfo

--
Thomas F. O'Connell
Co-Founder, Information Architect
Sitening, LLC
http://www.sitening.com/
110 30th Avenue North, Suite 6
Nashville, TN 37203-6320
615-260-0005

On Mar 17, 2005, at 1:57 PM, Richard Crawley wrote:

>
> Hello all.
>
> In the tail end of converting an app from MySQL to psql. I have this
> code :
>
>
> <snip>
> IF(days_complete <= -120, job_price,0) AS Days_120,
> IF(days_complete BETWEEN -119 AND -90, job_price,0) AS Days_90,
> IF(days_complete BETWEEN -89 AND -60, job_price,0) AS Days_60,
> IF(days_complete BETWEEN -59 AND -30, job_price,0) AS Days_30,
> IF(days_complete BETWEEN -29 AND 0, job_price,0) AS current
> <snip>
>
>
> It builds an aged debt report, and there are similar versions that
> SUM(IF ..) to give me debt by customer etc.
>
> All the questions I've seen about IF end up with people saying "use
> CASE" and I'm sure it would work. But do you lot really use 5 lines
> for each IF ? Doesn't it seem kind of ugly ? Or do you all secretly
> write a quick IF function ?
>
> I'm interested, and I half suspect that I'm missing a more elegant
> solution.
>
>
>
> thanks
>
>
> Rich
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings


pgsql-general by date:

Previous
From: Oleg Bartunov
Date:
Subject: Re: Tsearch2 index silently fails on PG 7.3.2
Next
From: Akbar
Date:
Subject: weird error on installing postgresql in Windows XP