Skip site navigation (1) Skip section navigation (2)

Include result of function call within WHERE clause in results

From: Dallas Morisette <p28flyer(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Include result of function call within WHERE clause in results
Date: 2009-05-17 00:44:42
Message-ID: (view raw or whole thread)
Lists: pgsql-novice
Hi all -

I need to select the rows of a table that satisfy a condition that  
includes a function call, and I want to include the result of the  
function call as a column in the resulting table. For example, this  
pseudo-code produces the result I'm looking for:

SELECT field1, some_function(...) FROM my_table
WHERE some_function(...) < some_number

However, I would like to avoid calling the function in two places,  
especially since embedded in the parameters to the function I have  
another SELECT command. Not to mention it just looks messy...

I've already tried the following without luck:

SELECT field1, some_function(...) AS result FROM my_table
WHERE result < some_number

Is there a way to cache the result of the function call so it can be  
used in both places without two separate calls?

Thanks in advance,
Dallas Morisette


pgsql-novice by date

Next:From: ...tharasDate: 2009-05-17 13:05:56
Previous:From: Tom LaneDate: 2009-05-16 19:16:03
Subject: Re: Custom sorting

Privacy Policy | About PostgreSQL
Copyright © 1996-2015 The PostgreSQL Global Development Group