Re: Getting 'n-1'th record.

Поиск
Список
Период
Сортировка
От Josh Berkus
Тема Re: Getting 'n-1'th record.
Дата
Msg-id web-105843@davinci.ethosmedia.com
обсуждение исходный текст
Ответ на Getting 'n-1'th record.  (Bhuvan A <bhuvansql@yahoo.com>)
Список 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

Вложения

В списке pgsql-sql по дате отправления:

Предыдущее
От: "Jeff Eckermann"
Дата:
Сообщение: Re: split/explode functions
Следующее
От: "Josh Berkus"
Дата:
Сообщение: Re: Primary vs Unique Index