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

Home -> Community -> Mailing Lists -> Oracle-L -> Any way to do this in straight SQL?

Any way to do this in straight SQL?

From: <ryan_gaffuri_at_comcast.net>
Date: Wed, 07 Dec 2005 21:13:20 +0000
Message-Id: <120720052113.6602.439750700006FCBE000019CA2205886360079D9A00000E09A1020E979D@comcast.net>


Is it possible to use lag, but you don't know how many rows you want to go back? create table history (

  history_id  number,
  history_sequence number,
  history_status varchar2(20),
  history_balance number);
insert into history(1,123,'HISTORY 1',10);
insert into history(1,128,'PROCESSED',0);
insert into history(1,130,'PROCESSED',0);
insert into history(1,131,'HISTORY 8',15); insert into history(1,145,'PROCESSED',0); for each history_id ordered by history_sequence   loop
    if status = 'PROCESSED' then
      history_balance = the history_balance of the last record where status != 'PROCESSED'     end if;
  end loop;
Typically with lag you have to state how many rows you are looking back, in this case my discriminator is based on the value in the status field?
After this is run, I expect the values to be
1,123,'HISTORY 1',10
1,128,'PROCESSED',10
1,130,'PROCESSED',10

1,131,'HISTORY 8',15
1,145,'PROCESSED',15
I can do this with pl/sql. I am trying to figure out how to do this with straight sql.
--
http://www.freelists.org/webpage/oracle-l
Received on Wed Dec 07 2005 - 15:13:34 CST

Original text of this message

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