Re: [GENERAL] SELECT LIMIT and the last rows of a query - Mailing list pgsql-general

From Jeff Davis
Subject Re: [GENERAL] SELECT LIMIT and the last rows of a query
Date
Msg-id 20000220021237.16270.qmail@web3002.mail.yahoo.com
Whole thread Raw
List pgsql-general
Try the folowing querys:

=> SELECT * FROM myTable ORDER BY myColumn DESC LIMIT
5;

That is exactly the last 5 rows of the query:

=> SELECT * FROM myTable ORDER BY myColumn;

Unfortunately, the tuples are in the opposite order
(thanks to DESC).

If the opposite order is a problem, I guess you could
do a workaround, like this:

=> CREATE VIEW myView AS SELECT * FROM myTable ORDER
BY myColumn DESC LIMIT 5;
=> SELECT * FROM myView ORDER BY myColumn ASC;
=> DROP VIEW myView;

I can't think of any problem with the second one
except speed, but i don't think there are any other
ways to do it; if there are I would like to know.

Hope it helps,
Jeff Davis

--- mathprof@bigfoot.com wrote:
> The command:
>
> SELECT (query) LIMIT m,n
>
> gives m rows of the query's response, starting at
> row n+1, while
>
> SELECT (query) LIMIT m
>
> gives the first m rows of the query's response
> (starting at row 1).
>
> Is there any easy way to get the *last* m rows of a
> query? I tried:
>
> SELECT (query) LIMIT 5, -5
>
> in an attempt to get the last 5 rows, but this gives
> a parser error. Any
> other thoughts?
>
>
> ************
>
>
__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

pgsql-general by date:

Previous
From: Tom
Date:
Subject: Backend terminated abnormally error ?
Next
From: "David May, Powered by FreeBSD, Somewhere in the Outback"
Date:
Subject: [Q] Tcl, ODBC and Postgresql.