Any way to do this in straight SQL?
- From: ryan_gaffuri@xxxxxxxxxxx
- To: oracle-l@xxxxxxxxxxxxx
- Date: Wed, 07 Dec 2005 21:13:20 +0000
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.
- Follow-Ups:
- Re: Any way to do this in straight SQL?
- From: malcolm arnold
Other related posts:
- » Any way to do this in straight SQL?
- » Re: Any way to do this in straight SQL?
- » Re: Any way to do this in straight SQL?
- » Re: Any way to do this in straight SQL?
- Re: Any way to do this in straight SQL?
- From: malcolm arnold