Search notes:

Oracle: Analytic functions

SUM and ORDER BY clause / Producing cumulative sum

sum(…) over (order by …) is equivalent to sum() over (order by … range between unbounded preceding and current row). A bit surprising to me, this default does not pruduce a cumulative sum. In order to produce such a sum, the clause order by … rows between unbounded preceding and current row must be used, as demonstrated in the following example:
alter session set nls_date_format = 'yyyy-mm-dd';

create table tq84_accum_sum (
    dt     date,
    val    number(4,2)
);
 
begin
   insert into tq84_accum_sum values (date '2023-01-06', 4.03);
   insert into tq84_accum_sum values (date '2023-01-09', 2.12);
   insert into tq84_accum_sum values (date '2023-01-09', 1.07);
   insert into tq84_accum_sum values (date '2023-01-12', 2.85);
 
   commit;
end;
/
 
select
   dt,
   val,
   sum(val) over(order by dt                                                  ) accum_sum_1,
   sum(val) over(order by dt range between unbounded preceding and current row) accum_sum_2,
   sum(val) over(order by dt rows  between unbounded preceding and current row) accum_sum_3
from
   tq84_accum_sum;
--
-- DT                VAL ACCUM_SUM_1 ACCUM_SUM_2 ACCUM_SUM_3
-- ---------- ---------- ----------- ----------- -----------
-- 2023-01-06       4.03        4.03        4.03        4.03
-- 2023-01-09       2.12        7.22        7.22        6.15
-- 2023-01-09       1.07        7.22        7.22        7.22
-- 2023-01-12       2.85       10.07       10.07       10.07


drop   table tq84_accum_sum;

See also

ratio_to_report
lag and lead
row_number, rank and dense_rank
first_value and nth_value
The WINDOW (SORT) SQL plan operation is used if the OVER() clause contains either an order by or partition by clause. Otherwise, the SQL plan operation WINDOW (BUFFER) is used.
A create table … as … that involves a database link and CLOBs or BLOBs and analytic functions sometimes(?) throws a ORA-22992: cannot use LOB locators selected from remote tables.
In order for a materialized view to be fast refreshable, the query must not have references analytic functions in the select clause.
SQL: Analytic functions
Improvements in 21c
The error messages
An SQL statement with analytic functions has an execution plan with a window operation.
Using analytic functions in a view prevents the view from being updatable.
The analytic and aggregate function checksum can be used to detect data changes in a table. Compare with dbms_comparison.

Index