Thread: Re: [HACKERS] calculated fields are not seen in the WHERE clause

Re: [HACKERS] calculated fields are not seen in the WHERE clause

From
"Henshall, Stuart - Design & Print"
Date:

Yaroslav Dmitriev wrote:
> Hello,
>
> OK
> select 1 as ccc where 1=1
>
> ERROR
> select 1 as ccc where ccc=1
> PostgreSQL said: ERROR: Attribute 'ccc' not found
>
> Is there any way to set conditions on calculated fields values?
>
> Best regards,
> Yar
>
No, the only way to do this is ot repeat the calculation.
If you wonder why here are a few reasons:
Aggregate functions - How can a where condition be based on something like MAX() or SUM()
Side effects of functions. Say for instance a have a function to delete things,
I might use the SELECT del_func(...) FROM tbl WHERE tm<'10/20/02';
If it didn't restrict first everything would get deleted.
Efficiency. If it had to base where conditions on calculated fields it would have to retreive all rows
that might fit, instead of say using an index to select the one row you really need.
hth,
- Stuart