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;