Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Cumulative sum (with reset)

Nuno RanitoMay 29 2017 — edited May 30 2017

Hello

Given the following data

with my_stat as

(

select 0 sales, '2010' year  from duaL

UNION

select 300, '2011' from duaL

UNION

select 0, '2012'  from duaL

UNION

select 0, '2013'  from duaL

UNION

select 123, '2014'  from duaL

UNION

SELECT 333, '2015' FROM DUAL

UNION

SELECT 0, '2016'  FROM DUAL

UNION

SELECT 333, '2017'  FROM DUAL

) select sales, year

  from my_stat 

  order by year;

 

I need a query to list sales for each year, and a column with sum of consecutive years without any sale. If there is a sale, the column returns to zero and starts counting again.

Sales  Year     No_sales

0         2010       1

300     2011       0

0         2012       1

0         2013       2

123    2014        0

333    2015        0

0        2016        1

333    2017        0

(Version: 11g Enterprise Edition Release 11.2.0.3.0)

Thank you for any help

This post has been answered by mathguy on May 29 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 27 2017
Added on May 29 2017
7 comments
655 views