用树形+eval_number进行计算

有这样一个需求,对于各行数据要求 1+2 1+2+3 1+2+3+4 —– 2+3 2+3+4 —- 28+29
上面的1 2 3 是行号
我们以emp表为例

WITH x0 AS
 (SELECT rownum AS rn, sal FROM (SELECT sal FROM emp ORDER BY sal))
SELECT sal,       dbms_aw.eval_number(substr(sys_connect_by_path(sal, '+'), 2))
  FROM x0
CONNECT BY nocycle(PRIOR rn) = rn - 1

可以看下相加前的部分效果

       800 800
       950 800+950
      1100 800+950+1100
      1250 800+950+1100+1250
      1250 800+950+1100+1250+1250
      1300 800+950+1100+1250+1250+1300
      1500 800+950+1100+1250+1250+1300+1500
      1600 800+950+1100+1250+1250+1300+1500+1600
      2450 800+950+1100+1250+1250+1300+1500+1600+2450
      2850 800+950+1100+1250+1250+1300+1500+1600+2450+2850
      2975 800+950+1100+1250+1250+1300+1500+1600+2450+2850+2975
      3000 800+950+1100+1250+1250+1300+1500+1600+2450+2850+2975+3000
      3000 800+950+1100+1250+1250+1300+1500+1600+2450+2850+2975+3000+3000
      5000 800+950+1100+1250+1250+1300+1500+1600+2450+2850+2975+3000+3000+5000
       950 950
      1100 950+1100
      1250 950+1100+1250
      1250 950+1100+1250+1250
      1300 950+1100+1250+1250+1300
      1500 950+1100+1250+1250+1300+1500
       SAL SUBSTR(SYS_CONNECT_BY_PATH(SAL

你可能感兴趣的:(树形查询)