Thread: Getting 'n-1'th record.

Getting 'n-1'th record.

From
Bhuvan A
Date:
hi all,

consider below..

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


HOW CAN I GET THIS?

Thankx in advance!
    ======================================================================    Q:    What's the difference between the
1950'sand the 1980's?    A:    In the 80's, a man walks into a drugstore and states loudly, "I'd       like some
condoms,"and then, leaning over the counter, whispers,                 "and some cigarettes."
======================================================================

Regards,
Bhuvaneswar.



Re: Getting 'n-1'th record.

From
"omid omoomi"
Date:
Hi,
It is a not a clean job but how about having a view like this :

create view foo_view as select * from yourtable order by oid desc limit 2 ;

and then making your select like this:
select * from foo_view order by oid limit 1;

hope that helps
Omid

>From: Bhuvan A <bhuvansql@yahoo.com>
>To: pgsql-sql@postgresql.org
>Subject: [SQL] Getting 'n-1'th record.
>Date: Tue, 21 Aug 2001 13:11:33 +0530 (IST)
>
>
>hi all,
>
>consider below..
>
>An sql query results with 'n' records. OK.
>I need ONLY the 'n-1'th record.
>
>
>HOW CAN I GET THIS?
>
>Thankx in advance!
>
>      
>======================================================================
>      Q:    What's the difference between the 1950's and the 1980's?
>      A:    In the 80's, a man walks into a drugstore and states loudly, "I'd
>         like some condoms," and then, leaning over the counter, whispers,
>                   "and some cigarettes."
>
>      
>======================================================================
>
>Regards,
>Bhuvaneswar.
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 4: Don't 'kill -9' the postmaster


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp



Re: Getting 'n-1'th record.

From
Tom Lane
Date:
Bhuvan A <bhuvansql@yahoo.com> writes:
> I need ONLY the 'n-1'th record.

See the 'LIMIT' and 'OFFSET' clauses in SELECT.  Note the caveat that
you'd better ORDER the rows to be sure you know which is the n-1'th.
Having done an ORDER BY, you could simplify your life by reversing
the ordering and choosing the second row (OFFSET 1 LIMIT 1).
        regards, tom lane


Re: Getting 'n-1'th record.

From
"Josh Berkus"
Date:
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

Re: Getting 'n-1'th record.

From
"Jeff Eckermann"
Date:
SELECT *
FROM table
ORDER BY field DESC
LIMIT 1
OFFSET 1;

This way you don't need to know the value of "n" in advance.
The descending ORDER BY is to indicate a reversal of your intended ordering,
so as to make the n-1'th record the second record.
Note that getting the n-1'th record from an unordered list will not give
meaningful results, because you cannot predict what will be returned.

----- Original Message -----
From: "Bhuvan A" <bhuvansql@yahoo.com>
To: <pgsql-sql@postgresql.org>
Sent: Tuesday, August 21, 2001 2:41 AM
Subject: Getting 'n-1'th record.


>
> hi all,
>
> consider below..
>
> An sql query results with 'n' records. OK.
> I need ONLY the 'n-1'th record.
>
>
> HOW CAN I GET THIS?
>
> Thankx in advance!
>
>
======================================================================
>   Q: What's the difference between the 1950's and the 1980's?
>      A: In the 80's, a man walks into a drugstore and states loudly, "I'd
>         like some condoms," and then, leaning over the counter, whispers,
>        "and some cigarettes."
>
>
======================================================================
>
> Regards,
> Bhuvaneswar.
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
>