sql - bounded Cumulative sum in Netezza -


i know how use cumulative sum in basic formulation, code this:

table name: employees dept_id salary ------------- 10     1000 10     1000 10     2000 10     3000 20     5000 20     6000 20     null  select dept_id,        salary,        sum(salary) over(partition dept_id               order salary asc               rows unbounded preceding) cum_sum   employees;  dept_id   salary  cum_sum   -------------------------- 10        1000    1000      10        1000    2000      10        2000    4000      10        3000    7000      20        5000    5000      20        6000    11000     20        null    11000     

but how limit cumulative sum n preceding rows? example, limit cumulative sum current row , previous 2 rows.

dept_id   salary  cum_sum   -------------------------- 10        1000    1000      10        1000    2000      10        2000    4000      10        3000    6000      20        5000    5000      20        6000    11000     20        null    11000     

the sql syntax is:

select dept_id,        salary,        sum(salary) over(partition dept_id               order salary asc               rows between <n> preceding , current row) cum_sum   employees; 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -