Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

use the preceeding calculated value in the next line in one execution or more

user12226330Sep 6 2013 — edited Sep 9 2013

This is a very complicated situation for me and I was wondering if someone can help me with it:

Here is my table:

In my table every value in the SQLCalculatedPmu column or desiredValue Column is calculated based on the preceding value.

As you can see, I have calculated the SQLcalculatedPMU column based on the round on 3 decimals. The case is that on each line radiation, the client want to start the next calculation based on 2 decimals instead of 3(represented in the desired values column). Next values will be recalculated. For example line 6 will change as the value in line 5 is now on 2 decimals. I could handle this if there where one single radiation but in my case I have a lot of Radiations and in this case they will change all based on the calculation of the two decimals.

In summary, Here are the steps:

investor
Record_no 

typepricenumberSoldeSQLCalculatedPmu  DesiredValues

10

2570088 Insertion  1336060   133 133
102636476 Insertion  0

7

67119,104   119,104
102636477 Insertion  0168117,352   117,352
102958292 Insertion  0674107,837   107,837
103148350 Radiation  0-173107,837   107,83
103282189 Insertion  078098,401   98,395
103646066 Insertion  08016049,201   49,198
103783510 Insertion  01617644,728   44,725
103783511 Insertion  0117744,475   44,472
104183663 Insertion  01118841,873   41,87
104183664 Insertion  0118941,651   41,648
104183665 Radiation  0-17341,651   41,64
104183666 Insertion  078040,156   40,145
104183667 Insertion  08016028,474   28,466
104183668 Insertion  01617626,908   26,901
104183669 Insertion  0117726,816   26,809
104183670 Insertion  01118825,842   25,836
104183671 Insertion  0118925,757   25,751

In my table every value in the SQLCalculatedPmu column or desiredValue Column is calculated based on the preceding value.

As you can see, I have calculated the SQLcalculatedPMU column based on the round on 3 decimals. The case is that on each line radiation, the client want to start the next calculation based on 2 decimals instead of 3(represented in the desired values column). Next values will be recalculated. For example line 6 will change as the value in line 5 is now on 2 decimals. I could handle this if there where one single radiation but in my case I have a lot of Radiations and in this case they will change all based on the calculation of the two decimals.

In summary, Here are the steps:

1 - round the value of the preceding row of a raditaiton and put it in the radiation row.
2 - calculate all next insertion rows.
3 - when we reach another radiation we redo steps 1 and 2 and so on

Im using an oracle DB and I m the owner so I can make procedures, insert, update, select. But I m not familiar with procedures or loops.

For information, this is the formula for SQLCalculatedPmu uses two additional culmns price and number and this is calculated every line cumulativelly for each investor:

  (price * number)+(cumulative (price*number) of the preceeding lines)

tried something like this :

update PMUTemp

  
set SQLCalculatedPmu =
  
case when Type = 'Insertion' then
  
(number*price)+lag(SQLCalculatedPmu ,1) over (partition by investor
  
order by  Record_no)/
  
(number+lag(solde,1) over (partition by investor order by Record_no))
  
else
  TRUNC
(lag(SQLCalculatedPmu,1) over partition by invetor order by  Record_no))
  
end;


but I gave me this error (I think it's because I m looking at the preceiding line that itself is modified during the SQL statement) :


ORA-30486 : window function are allowed only in the SELECT list of a query.


was wondering if creating a procedure that will be called as many time as the number of radiations would do the job but I m really not good in procedures


Any help Regards,

This post has been answered by chris227 on Sep 8 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 7 2013
Added on Sep 6 2013
14 comments
565 views