Re: Getting 'n-1'th record. - Mailing list pgsql-sql

From Josh Berkus
Subject Re: Getting 'n-1'th record.
Date
Msg-id web-105843@davinci.ethosmedia.com
Whole thread Raw
In response to Getting 'n-1'th record.  (Bhuvan A <bhuvansql@yahoo.com>)
List pgsql-sql
Bhuvan,

> An sql query results with 'n' records. OK.
> I need ONLY the 'n-1'th record.

You're making this much harder than it needs to be.

If you want the "nth" record, then you have to be supplying the database
with an ORDER BY. For the next-to-last record, simply reverse the ORDER
BY and take the second record.

e.g.:

If you want the next-to-last (n-1) record from:
SELECT * FROM syslog
ORDER BY entrytime;

Then ask for:
SELECT * FROM syslog
ORDER BY entrytime DESC
LIMIT 1 OFFSET 1;

Easy, no?

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco

Attachment

pgsql-sql by date:

Previous
From: "Jeff Eckermann"
Date:
Subject: Re: split/explode functions
Next
From: "Josh Berkus"
Date:
Subject: Re: Primary vs Unique Index