Solved

cumulative figures in oracle sql

Posted on 2011-02-23
3
837 Views
Last Modified: 2012-06-22
Is it possible to calculate cumulatively in oracle, i.e. using the results of the last row for calculating the values in the current row?
For example I want my query to return the following:
Month      col1      _      col2      _      col3
jan      1      _      100      _      100 * 1
feb      2      _      200      _      100 * 2 + 200
mar      3      _      300      _      400 * 3 + 300
apr      4      _      400      _      1500 * 4 + 400
may      5      _      500      _      6400 * 5 + 500

In col3 above, for feb I want to use the result returned for jan ((100 * 1)*2+200),
for mar I am using the result returned for feb((100 * 2 + 200) * 3 + 300) and so on.

i.e. I want to use the previous value of column3 to derive the current value of column3.
Like using the LAG function but on the analytically derived column itself.

Thanks in advance.

0
Comment
Question by:subratoc
  • 2
3 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 34967752
yes use LAG function
LAG (<sql_expr>, <offset>, <default>) OVER (<analytic_clause>)

HTH

Ivo Stoykov

PS: here there is some help about
SELECT deptno, empno, sal,
LEAD(sal, 1, 0) OVER (PARTITION BY dept ORDER BY sal DESC NULLS LAST) NEXT_LOWER_SAL,
LAG(sal, 1, 0) OVER (PARTITION BY dept ORDER BY sal DESC NULLS LAST) PREV_HIGHER_SAL
FROM emp
WHERE deptno IN (10, 20)
ORDER BY deptno, sal DESC;

 DEPTNO  EMPNO   SAL NEXT_LOWER_SAL PREV_HIGHER_SAL
------- ------ ----- -------------- ---------------
     10   7839  5000           2450               0
     10   7782  2450           1300            5000
     10   7934  1300              0            2450
     20   7788  3000           3000               0
     20   7902  3000           2975            3000
     20   7566  2975           1100            3000
     20   7876  1100            800            2975
     20   7369   800              0            1100

8 rows selected.

Open in new window

0
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 500 total points
ID: 34967801
we can easily do it with a function :

create or replace function fun2_test( c1 number ) return number is
temp_var number;
type col1_type is table of number index by binary_integer;
col1_arr col1_type;
type col2_type is table of number index by binary_integer;
col2_arr col1_type;
begin
select col1, col2 bulk collect into col1_arr, col2_arr from ttt where col1 <= c1 order by col1;
for x in col1_arr.first .. col1_arr.last
loop
temp_var := nvl(temp_var,0) * col1_arr(x) + col2_arr(x) ;
end loop;
return temp_var;
end;
/

select ttt.*, fun2_test(col1) mytotal
from ttt;

see the output in the doc attached.

I believe this can even be done in a sql query using LAG/SUM analytical functions.
ee.doc
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34967883
I think in this case function is better and you can easily modify it in future for any changes/modifications though it may be possible to directly do it with a SQL query but the complexity of the SQL query will end in difficult situation to modify for future needs.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Encryption Decryption in Oracle 12 122
grouping on time windows 6 51
Oracle - SQL Parse String 5 34
exp/imp 25 75
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question