Search Postgresql Archives

Re: Using column aliasses in the same query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2011/4/17 Robert J.C. Ivens <robert@xxxxxxxxxxx>:
>
> On 17 apr 2011, at 13:21, Leif Biberg Kristensen wrote:
>
>> On Sunday 17 April 2011 13:01:45 Robert J.C. Ivens wrote:
>>> Hi,
>>>
>>> I am not sure if there ever was a feature request for using defined column
>>> aliases in the rest of a query. This would make queries with a lot of
>>> logic in those aliased columns a lot smaller and this easier to
>>> write/debug.
>>>
>>> I already know you can use the following syntax:
>>>
>>> SELECT col1, col2, col3, (col2-col3) as col4 FROM (SELECT col1, (long and
>>> lots of logic here) as col2, col3 FROM table) s WHERE col2 < aValue
>>>
>>> But when you need to use (calculated) values from the actual record and or
>>> have sub-selects in your main select that also need to use these values
>>> things get really hairy. I don't know if the SQL specification allows it
>>> but I know that RDBMS's like Sybase already support this.
>>>
>>> Any thoughts?
>>
>> It's easy to define a view or an SQL function and stash the hairy logic there.
>>
>> regards, Leif
>
> True, but that is essentially the same thing as the example query I gave.
> There are plenty of cases where this approach is not workable.

select bar.*, b-c
from (select i,i,i from foo )
          as bar(a,b,c)
where c!=1;

you can also look at:
http://www.postgresql.org/docs/9.0/static/queries-with.html


-- 
Cédric Villemain               2ndQuadrant
http://2ndQuadrant.fr/ ;    PostgreSQL : Expertise, Formation et Support

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux