sql - Cumulative Sum of last 12 months -
i trying make cumulative sum of last 12 months so, example on 201702 should show sum of 201603 201702.
right have following query:
select date, product, sum(sales) on (partition product order date) cumulative sales
the data this:
date product sales sales acum 201601 1 7648 7648 201602 1 5538 13186 201603 1 7659 20845 201604 1 6943 27788 201605 1 7604 35392 201606 1 4398 39790 201607 1 3261 43051 201608 1 3040 46091 201609 1 5637 51728 201610 1 5520 57248 201611 1 8554 65802 201612 1 4794 70596 201701 1 6704 69652 201702 1 2234 66348 201703 1 4093 62782 201704 1 4171 60010 201705 1 6741 59147 201706 1 2902 57651 201601 2 582 582 201602 2 2393 100416 201603 2 4614 105030 201604 2 2611 107641 201605 2 6891 114532 201606 2 4409 118941 201607 2 5454 124395 201608 2 7927 132322 201609 2 6797 139119 201610 2 6740 145859 201611 2 8077 153936 201612 2 5143 159079 201701 2 6383 67439 201702 2 1593 66639 201703 2 5352 67377 201704 2 4065 68831 201705 2 7434 69374 201706 2 2332 67297
if want information sales during last year, appropriate condition in where
clause should present. oracle can use following query:
select date, product, sum(sales) on (partition product order date) cumulative sales date >= add_months(sysdate, -12)
Comments
Post a Comment