[Tip] Cumulated Total Column

Dúvidas, dicas e truques de SQL, Select, Update, Delete, cláusulas, operações com joins, Funções em SQLs, etc
Post Reply
User avatar
dr_gori
Moderador
Moderador
Posts: 5024
Joined: Mon, 03 May 2004 3:08 pm
Location: Portland, OR USA
Contact:
Thomas F. G

Você já respondeu a dúvida de alguém hoje?
https://glufke.net/oracle/search.php?search_id=unanswered

Look at this SQL - shows the accumulated total of the column! (without using analytical function)

Select all

SELECT  
  B.SAL 
, SUM(A.SAL) ACUMULADO 
FROM  
  EMP A 
, EMP B 
WHERE A.ROWID <= B.ROWID 
GROUP BY B.ROWID, B.SAL
there is an example:

Select all

SQL> SELECT  
  2    B.SAL 
  3  , SUM(A.SAL) ACUMULADO 
  4  FROM  
  5    EMP A 
  6  , EMP B 
  7  WHERE A.ROWID <= B.ROWID 
  8  GROUP BY B.ROWID, B.SAL 
  9  / 
 
       SAL  ACUMULADO 
---------- ---------- 
       800        800 
      1600       2400 
      1250       3650 
      2975       6625 
      1250       7875 
      2850      10725 
      2450      13175 
      3000      16175 
      5000      21175 
      1500      22675 
      1100      23775 
       950      24725 
      3000      27725 
      1300      29025 
                29025 
 
15 rows selected. 
 
SQL> 
Last edited by dr_gori on Thu, 15 Sep 2005 11:48 am, edited 1 time in total.
User avatar
leobbg
Rank: Programador Júnior
Rank: Programador Júnior
Posts: 22
Joined: Fri, 29 Oct 2004 10:25 am
Location: PORTO ALEGRE - RS
Leo BBG Consultor Oracle

Only for those who like to invent fashion .. but who does not like it? Complicating or uncomplicating .. there will another very nice analytical function ... which has the same effect.

Select all

SELECT A.SAL 
,      SUM( A.SAL ) OVER( ORDER BY A.SAL ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) ACUMULADO 
FROM   EMP A 
GROUP BY A.SAL
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 5 guests