Let's say I want to write a simple SELECT
query that uses a VIEW
:
CREATE TEMP VIEW people AS
SELECT
p.person_id
,p.full_name
,p.phone
FROM person p
ORDER BY p.last_name;
SELECT
p.*
,h.address
,h.appraisal
FROM people p
LEFT JOIN homes h
ON h.person_id = p.person_id
ORDER BY p.last_name, h.appraisal;
The obvious problem here is that p.last_name
is no longer available when I go to perform the final ORDER BY
.
How can I sort the final query so that the original sequence of the people
view follows through to the final query?
The simple solution here, is to just include p.last_name with the view. I don't want to do that - my real world example (much more complicated) makes that a problem.
I've done similar things with temp tables in the past. For example, I create the table with CREATE TEMP TABLE testing WITH OIDS
and then do an ORDER BY testing.oid
to pass through the original sequence.
Is it possible to do the same with views?