Home > database >  How to realize the first few lines accumulation, please?
How to realize the first few lines accumulation, please?

Time:10-05

A 1
2 b
3 c
4 d
E 5
6 f
The above data, to realize the function of line according to the accumulation, get the following data:
A 1 1
2, 3, b
3 6 c
D 4 10
E 5 15
F 6 21

CodePudding user response:

 
SQL> Create table test (id varchar (10), num int);
The Table created
SQL> The begin
2 insert into the test values (' a ', 1);
3 the insert into the test values (" b ", 2);
4 the insert into the test values (' c ', 3);
5 the insert into the test values (' d ', 4);
6 insert into the test values (' e ', 5);
7 the insert into the test values (' f ', 6);
8 the end;
9/
PL/SQL procedure successfully completed
SQL> Select id, num, sum (num) over (order by id) sum_num
2 the from the test;
ID NUM SUM_NUM
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
A 1 1
2, 3, b
3 6 c
D 4 10
E 5 15
F 6 21
6 rows selected
SQL> Drop table test purge;
Table dropped

SQL>

CodePudding user response:

If num column is not a natural growth column, it should use a window function:
Select id, num, sum (num) over (order by rownum rows between preceding and current row) sum_num from test;

CodePudding user response:

reference 1st floor wmxcn2000 response:
 
SQL> Create table test (id varchar (10), num int);
The Table created
SQL> The begin
2 insert into the test values (' a ', 1);
3 the insert into the test values (" b ", 2);
4 the insert into the test values (' c ', 3);
5 the insert into the test values (' d ', 4);
6 insert into the test values (' e ', 5);
7 the insert into the test values (' f ', 6);
8 the end;
9/
PL/SQL procedure successfully completed
SQL> Select id, num, sum (num) over (order by id) sum_num
2 the from the test;
ID NUM SUM_NUM
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
A 1 1
2, 3, b
3 6 c
D 4 10
E 5 15
F 6 21
6 rows selected
SQL> Drop table test purge;
Table dropped

SQL>



Excuse me the moderator why to use PLSQL grammar before insert can't insert it directly

CodePudding user response:

reference thyrozol reply: 3/f
excuse me moderator why to use PLSQL grammar before insert can't insert it directly


Can also, this is a habit;

If you don't write a begin, end, each insert statement, will appear a "insert row" such a hint, look more chaotic;

CodePudding user response:

reference 4 floor wmxcn2000 response:
Quote: reference thyrozol reply: 3/f


Excuse me the moderator why to use PLSQL grammar before insert can't insert it directly


Can also, this is a habit;

If you don't write a begin, end, each insert statement, will appear a "insert row" such a hint, look more chaotic;


+ 1
This is a good habit to

CodePudding user response:

reference 5 floor jdsnhan reply:
+ 1
This is a good habit to


Master, so early!

CodePudding user response:

Learning, written by Oracle and found the way of how to realize the accumulative sum in SQL Server
  • Related