Re: Hey! ORDER BY in VIEWS?

From: Richard Huxton <dev(at)archonet(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Hey! ORDER BY in VIEWS?
Date: 2001-07-16 06:38:08
Message-ID: 3B528BCF.92DEC4BB@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Josh Berkus wrote:
>
> Pater, Robbie, Bruce,
>
> Makes sense. I take it that this is a deviation from the ANSI 92
> standard, then?
>
> What happens if I put an ORDER BY in a view, then call an ORDER BY in a
> query, e.g.:

> Does the second ORDER BY override or suppliment the view ORDER BY, or is
> it ignored?

It overrides.

People seem to be forgetting ORDER BY ... LIMIT has selective qualities
as well as ordering ones.

The example someone gave me was when you use LIMIT ... OFFSET to fetch
results a page at a time. If you want the last page of your results you
need to do something like:

SELECT * FROM messages
ORDER BY msg_timestamp DESC
LIMIT 20;

But - this gives them in reverse timestamp order. So - wrap the query in
a view and then apply your own ORDER BY.

Can't remember who came up with this (some evil genius :-) - but it
seemed to make sense so I stuck the example in my PostgreSQL notes.

- Richard Huxton

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Bhuvan A 2001-07-16 08:57:13 SELECT * from select - HOW?
Previous Message Stephan Szabo 2001-07-16 03:48:32 Re: Unknown values in int8 fields?