1. At first create a table: cumulative_sum
create table cumulative_sum
(
instr_date date,
instr_id number,
amount number
);
2. Insert some Data:
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate-1, 1, 100);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate-1, 2, 100);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate-1, 5, 300);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate-1, 3, 200);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate+1, 100, 50);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate+1, 200, 75);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate+1, 400, 60);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate+1, 300,10);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate, 1, 100);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate, 2, 100);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate, 3, 600);
insert into cumulative_sum(instr_date, instr_id, amount)
values(sysdate, 5, 700);
commit;
3. Select data from table :
SELECT * FROM cumulative_sum;
4. Select data with running total from the table :
SELECT a.*, sum(amount) over (order by rowid range between unbounded preceding and current row)
running_total
FROM cumulative_sum a;
5. Select data from the table order by instr_date, instr_id:
SELECT * FROM cumulative_sum order by instr_date, instr_id;
6. Select data with running total from the table order by instr_date, instr_id:
running_total
FROM cumulative_sum a order by instr_date, instr_id;
No comments:
Post a Comment