Home » SQL & PL/SQL » SQL & PL/SQL » Help - Complex Query
Help - Complex Query [message #20535] Mon, 03 June 2002 05:12 Go to next message
Sulaiman Gboyega N.
Messages: 17
Registered: April 2002
Junior Member
Hi All,

Please I will appreciate your help insolving the scenario I described below:

Item Date Value1 Value2 Target
-------------------------------------------------------
AA1 | 01/01/2002 | 500 | 1500 | 1500
AA1 | 01/02/2002 | 700 | 1000 | 1000
AA1 | 01/03/2002 | 0 | 0 | 1000
AA1 | 01/04/2002 | 0 | 0 | 1000
AA1 | 01/05/2002 | 900 | 1900 | 1900
AA1 | 01/06/2002 | 0 | 0 | 1900
AA1 | 01/07/2002 | 400 | 2300 | 2300

AA2 | 01/01/2002 | 300 | 900 | 900
AA2 | 01/02/2002 | 0 | 0 | 900
AA2 | 01/03/2002 | 600 | 100 | 100
AA2 | 01/04/2002 | 0 | 0 | 100
AA2 | 01/05/2002 | 500 | 600 | 600
AA2 | 01/06/2002 | 0 | 0 | 600
AA2 | 01/07/2002 | 800 | 1400 | 1400

As described in the table above, I have fields Date, Value1 and Value2 and I want to get another field (Target) which contains the amount in Value2 but replaces zeros in value2 with the previous amount of value2.

Thanks for your help.

Sulaiman.
Re: Help - Complex Query [message #20555 is a reply to message #20535] Tue, 04 June 2002 09:58 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
Hello,
here is an example, with the standard demo table emp

SQL> select empno,sal,comm from emp;

     EMPNO        SAL       COMM
---------- ---------- ----------
      7499       1600      21087
      7521       1250      21210
      7566       2975      21333
      7654       1250      21456
      7698       2850      21579
      7782       2450          0
      7788       3000      21825
      7839       5000          0
      7844       1500      22071
      7876       1100      22194
      7900        950      22317
      7902       3000      22440
      7934       1300          0

13 rows selected.

SQL> get su
line 23 truncated.
  1  create or replace procedure su
  2  as
  3     CURSOR C1 is SELECT EMPNO,SAL,nvl(comm,0) comm FROM EMP;
  4     new number;
  5  BEGIN
  6     dbms_output.put_line('empno     sal     comm    new');
  7     dbms_output.put_line('-----------------------------');
  8     for mag in c1 loop
  9     exit when c1%notfound;
 10     if mag.comm != 0 then
 11             new:=mag.comm;          
 12             dbms_output.put_line(mag.empno||'       '
 13             ||mag.sal||'    '||mag.comm||'  '||new);
 14     END     if;
 15     if mag.comm=0 then
 16             mag.comm:=new;
 17             dbms_output.put_line(mag.empno||'       '
 18             ||mag.sal||'    '||mag.comm||'  '||new);
 19     END     if;     
 20     END     loop;
 21* end;
 22  /

Procedure created.

SQL> exec su
empno   sal     comm    new
-----------------------------
7499    1600    21087   21087
7521    1250    21210   21210
7566    2975    21333   21333
7654    1250    21456   21456
7698    2850    21579   21579
7782    2450    21579   21579
7788    3000    21825   21825
7839    5000    21825   21825
7844    1500    22071   22071
7876    1100    22194   22194
7900    950     22317   22317
7902    3000    22440   22440
7934    1300    22440   22440

PL/SQL procedure successfully completed.

SQL> 
Re: Help - Complex Query [message #20593 is a reply to message #20535] Thu, 06 June 2002 03:10 Go to previous message
shyampaliyath
Messages: 22
Registered: May 2002
Junior Member
hai sulaiman,

I couldn't able to get your question. what do u mean by preavious value of value2??

with regards
Shyam
Previous Topic: heu....help
Next Topic: stoping of a procedure
Goto Forum:
  


Current Time: Tue Apr 23 13:12:03 CDT 2024