Skip site navigation (1) Skip section navigation (2)

Re: Selecting the last 2 rows of a table

From: Thom Brown <thom(at)linux(dot)com>
To: Machiel Richards <machielr(at)rdc(dot)co(dot)za>
Cc: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Selecting the last 2 rows of a table
Date: 2010-11-09 09:42:22
Message-ID: AANLkTin-wf_NLbttk+OzzL-yFfTr96h9PHCMgg-7Bq_D@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-novice
On 9 November 2010 09:34, Machiel Richards <machielr(at)rdc(dot)co(dot)za> wrote:

>  Good day all
>
>      I am trying to find out how I can select the last 2 or (n amount ) of
> rows from a specific table.
>
>       We have a table which has been growing rediculously the last view
> days (I must say after a code go-live).
>
>        I am trying to find out what data is being written to the table that
> is causing this, however due to the size of the table at the moment, it can
> cause problems so I am just interested in the last number of records.
>
>
>       I would appreciate any assistance in this.
>

Well, you'll have to decide what determines the order of your tuples.  If
you have a column bound to a sequence, and that is considered to put them in
order, then it would be

SELECT my_columns
FROM my_table
ORDER BY sequence_column DESC
LIMIT 2

Or you could replace that with a date field if appropriate.  But whatever
column you use, you'd need an index on it.

-- 
Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

In response to

pgsql-novice by date

Next:From: Majid AzimiDate: 2010-11-09 09:48:42
Subject: Re: Selecting the last 2 rows of a table
Previous:From: Machiel RichardsDate: 2010-11-09 09:34:48
Subject: Selecting the last 2 rows of a table

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group