gpt4 book ai didi

sql - Netezza 中的有界累积和

转载 作者:行者123 更新时间:2023-12-01 11:30:04 28 4
gpt4 key购买 nike

我知道如何在其基本公式中使用累积总和,代码如下:

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 BY dept_id
ORDER BY salary ASC
rows unbounded preceding) cum_sum
FROM 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

但是如何将累积总和限制为仅前 N 行?
例如,将累积总和限制为当前行和前两行。
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

最佳答案

SQL 语法是:

SELECT dept_id,
salary,
SUM(salary) OVER(PARTITION BY dept_id
ORDER BY salary ASC
rows between <N> preceding and current row) cum_sum
FROM Employees;

关于sql - Netezza 中的有界累积和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32873835/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com