Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL to show window around selected records

Re: SQL to show window around selected records

From: Andrew Allen <andrew.allen_at_handleman.com>
Date: Wed, 19 Feb 2003 21:40:59 GMT
Message-ID: <3E53ECDE.2090403@handleman.com>


Lookup LEAD and LAG in the SQL reference manual for 9i. LEAD and LAG should do exactly what you want.

--
Anderw Allen

Brett Hunsaker wrote:

> I'd like to display records both preceding and following a record in an Oracle
> 9i database.
>
> Given a table named EVENTS with the following layout:
>
> Name Null? Type
> ------------------ -------- --------------
> LOGSEQUENCE NOT NULL NUMBER(38)
> LOGTIMESTAMP NOT NULL DATE
> DESCRIPTION VARCHAR2(255)
>
> I want to display the 4 records preceding and following a record (when
> sorted by the LogSequence column) where the description text begins with
> "BAD THING HAPPENED".
>
> select * from events where description like 'BAD THING HAPPENED%'
> order by logsequence;
>
> gets the center record.
>
> 'LogSequence' is a monotonically increasing number.
>
> Can this be done in a single SQL statement? Examples would be appreciated.
>
> Thanks!
Received on Wed Feb 19 2003 - 15:40:59 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US