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
Post a Comment