首页 > 代码库 > oracle当前月添加一列显示前几个月的累计值

oracle当前月添加一列显示前几个月的累计值

create table test_leiji(rpt_month_id number(8),
                        current_month NUMBER(12,2));

insert into test_leiji (RPT_MONTH_ID, CURRENT_MONTH) values (200901, 380128.344363);
insert into test_leiji (RPT_MONTH_ID, CURRENT_MONTH) values (200902, 274474.595772);
insert into test_leiji (RPT_MONTH_ID, CURRENT_MONTH) values (200903, 309427.898521);
insert into test_leiji (RPT_MONTH_ID, CURRENT_MONTH) values (200904, 379854.833875);

insert into test_leiji (RPT_MONTH_ID, CURRENT_MONTH) values (201001, 380128.344363);
insert into test_leiji (RPT_MONTH_ID, CURRENT_MONTH) values (201002, 274474.595772);
insert into test_leiji (RPT_MONTH_ID, CURRENT_MONTH) values (201003, 309427.898521);
insert into test_leiji (RPT_MONTH_ID, CURRENT_MONTH) values (201004, 379854.833875);


select  * from  test_leiji t


SELECT rpt_month_id,
       current_month,
       SUM (current_month) OVER (PARTITION BY SUBSTR (rpt_month_id, 1, 4)order by rpt_month_id) sum_month
  FROM test_leiji
  order by rpt_month_id;

显示结果如图:

oracle当前月添加一列显示前几个月的累计值