Re: Last x records

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Last x records
Дата
Msg-id 29338.983289696@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: Last x records  ("Richard Huxton" <dev@archonet.com>)
Список pgsql-general
"Richard Huxton" <dev@archonet.com> writes:
> From: "Matthias Teege" <matthias@mteege.de>
>> is there any way to get the last x records of an query
>> result?

> Otherwise, the only thing that I can think of is to set up a view that does
> the LIMIT n on a DESC then sort ASC when selecting on that view - might
> work.

Not in 7.0.* or before --- there is just plain no way to get multiple
levels of sorting in a query.  In 7.1 you can do it like this:

    SELECT * FROM
        (SELECT ... ORDER BY foo DESC LIMIT n) ss
    ORDER BY foo;

A workaround in 7.0.* is to use a temp table:

    SELECT ... INTO TEMP t1 ... ORDER BY foo DESC LIMIT n;
    SELECT * FROM t1 ORDER BY foo;
    DROP TABLE t1;

The extra sort step shouldn't affect the speed much as long as n is small.

            regards, tom lane

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

Предыдущее
От: Marc SCHAEFER
Дата:
Сообщение: Re: Re: Slowdown problem when writing 1.7million records
Следующее
От: Joel Burton
Дата:
Сообщение: Re: Case sensitivity