dbTalk Databases Forums  

Iterative Calculations

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Iterative Calculations in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Hans Mayr
 
Posts: n/a

Default Iterative Calculations - 08-14-2009 , 07:16 AM






Hello,

I have a table that contains the fields PERIOD, STOCK, NEW_UNITS,
CHURN_RATE. STOCK is only available for a given start period. I want
to update the expected stock of the next periods. Is there a way that
in pure SQL (without PL SQL and a cursor) I can calculate

STOCK of PERIOD (N) = STOCK of PERIOD (N-1) * (1-CHURN_RATE) +
NEW_UNITS

?

Thanks and best,

Hans

Reply With Quote
  #2  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: Iterative Calculations - 08-14-2009 , 07:21 AM






Hans Mayr escribió:
Quote:
I have a table that contains the fields PERIOD, STOCK, NEW_UNITS,
CHURN_RATE. STOCK is only available for a given start period. I want
to update the expected stock of the next periods. Is there a way that
in pure SQL (without PL SQL and a cursor) I can calculate

STOCK of PERIOD (N) = STOCK of PERIOD (N-1) * (1-CHURN_RATE) +
NEW_UNITS
Perhaps you can use the LAG function:

http://www.adp-gmbh.ch/ora/sql/analytical/lag.html


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
  #3  
Old   
Hans Mayr
 
Posts: n/a

Default Re: Iterative Calculations - 08-14-2009 , 07:30 AM



On 14 Aug., 14:21, "Álvaro G. Vicario"
<alvaro.NOSPAMTH... (AT) demogracia (DOT) com.invalid> wrote:
Quote:
Hans Mayr escribió:

I have a table that contains the fields PERIOD, STOCK, NEW_UNITS,
CHURN_RATE. STOCK is only available for a given start period. I want
to update the expected stock of the next periods. Is there a way that
in pure SQL (without PL SQL and a cursor) I can calculate

STOCK of PERIOD (N) = STOCK of PERIOD (N-1) * (1-CHURN_RATE) +
NEW_UNITS

Perhaps you can use the LAG function:
Hi,

no, I don't think so. Because the lag goes to a fiel in the preceeding
line of a certain object. But I need to refer to a calculation,
actually the calculation has to refer to itself. Maybe I was not clear
but I do not only want to calculate the Stock for one following period
but for many. So the result n has to become the source of result n+1
etc.

If I did not express myself clearly enough, please tell me.

Best,

Hans

Reply With Quote
  #4  
Old   
Michel Cadot
 
Posts: n/a

Default Re: Iterative Calculations - 08-14-2009 , 09:54 AM



"Hans Mayr" <mayr1972 (AT) gmx (DOT) de> a écrit dans le message de news: 175b9dc2-db4a-4fab-855a-276439402249...oglegroups.com...
Quote:
Hello,

I have a table that contains the fields PERIOD, STOCK, NEW_UNITS,
CHURN_RATE. STOCK is only available for a given start period. I want
to update the expected stock of the next periods. Is there a way that
in pure SQL (without PL SQL and a cursor) I can calculate

STOCK of PERIOD (N) = STOCK of PERIOD (N-1) * (1-CHURN_RATE) +
NEW_UNITS

?

Thanks and best,

Hans
If you post a test case (create table and insert statements) maybe we can help more.
It seems to be a good example for MODEL clause although it may be possible to do
it with CONNECT BY depending on the exact case.

Regards
Michel

Reply With Quote
  #5  
Old   
Michel Cadot
 
Posts: n/a

Default Re: Iterative Calculations - 08-14-2009 , 10:29 AM



"Michel Cadot" <micadot{at}altern{dot}org> a écrit dans le message de news: 4a857ab2$0$287$426a74cc (AT) news (DOT) free.fr...
Quote:
"Hans Mayr" <mayr1972 (AT) gmx (DOT) de> a écrit dans le message de news:
175b9dc2-db4a-4fab-855a-276439402249...oglegroups.com...
| Hello,
|
| I have a table that contains the fields PERIOD, STOCK, NEW_UNITS,
| CHURN_RATE. STOCK is only available for a given start period. I want
| to update the expected stock of the next periods. Is there a way that
| in pure SQL (without PL SQL and a cursor) I can calculate
|
| STOCK of PERIOD (N) = STOCK of PERIOD (N-1) * (1-CHURN_RATE) +
| NEW_UNITS
|
| ?
|
| Thanks and best,
|
| Hans

If you post a test case (create table and insert statements) maybe we can help more.
It seems to be a good example for MODEL clause although it may be possible to do
it with CONNECT BY depending on the exact case.

Regards
Michel


See the discussion in the following thread:
http://www.orafaq.com/forum/mv/msg/1...89/#msg_350586

Regards
Michel

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.