Selecting Fields in Union in Subquery - Mailing list pgsql-novice

From Tom Burns
Subject Selecting Fields in Union in Subquery
Date
Msg-id 1342216174.12212.YahooMailNeo@web122505.mail.ne1.yahoo.com
Whole thread Raw
Responses Re: Selecting Fields in Union in Subquery  (Bartosz Dmytrak <bdmytrak@gmail.com>)
List pgsql-novice
Hi All Hope You Can Help,

I have a query that selects from a sub-query:
    select *
    from
    (select g.geo_id as "Id", g.geo_name as "Country", gTo.geo_name as "StateProvence"
    from geo g,geo gTo, geo_assoc ga
    where ga.geo_id = g.geo_id
    and ga.geo_id_to = gTo.geo_id
    and g.geo_id IN (select geo_id from geo where geo_type_id='COUNTRY')
    and g.geo_id = 'USA'
    union
    select g.geo_id as "Id", g.geo_name as "Country", '' as "State/Provence"
    from geo g LEFT OUTER JOIN geo_assoc ga on g.geo_id = ga.geo_id
    where g.geo_type_id='COUNTRY' and ga.geo_id is null
    and g.geo_id = 'USA') as GeoList
    order by 3;

I would like to filter and order by fields returned by the sub-query . For example;
from(select... clause.
The order "order by 3;"works

I can no figure out the syntax for directly addressing fields (for example 'order by StateProvence" or "order by gTo.geo_name" instead of order by 3)
I would like to be able to filter and sort the sub-query in the parent query.

Thanks for any help,

Tom

pgsql-novice by date:

Previous
From: "ktm@rice.edu"
Date:
Subject: Re: How to apply a regular expression to a select statement
Next
From: Ioannis Anagnostopoulos
Date:
Subject: Index slow down insertions...