gpt4 book ai didi

postgresql - 带有 postgres 窗口函数的重复行

转载 作者:行者123 更新时间:2023-11-29 11:29:24 27 4
gpt4 key购买 nike

postgres 文档 ( http://www.postgresql.org/docs/9.1/static/tutorial-window.html ) 讨论了窗口函数。

在他们的例子中:

SELECT salary, sum(salary) OVER (ORDER BY salary) FROM empsalary;

重复的处理如下:

 salary |  sum  
--------+-------
3500 | 3500
3900 | 7400
4200 | 11600
4500 | 16100
4800 | 25700 <-- notice duplicate rows have the same value
4800 | 25700 <-- SAME AS ABOVE
5000 | 30700
5200 | 41100 <-- same as next line
5200 | 41100 <--
6000 | 47100
(10 rows)

你如何做同样的事情,使重复的​​行不被赋予相同的值?换句话说,我希望这张表如下所示:

 salary |  sum  
--------+-------
3500 | 3500
3900 | 7400
4200 | 11600
4500 | 16100
4800 | 20900 <-- not the same as the next line
4800 | 25700 <--
5000 | 30700
5200 | 35900 <-- not the same as the next line
5200 | 41100 <--
6000 | 47100
(10 rows)

最佳答案

在框架子句中使用 rows 而不是默认的 range

select
salary,
sum(salary) over (
order by salary
rows unbounded preceding
)
from empsalary
;
salary | sum
--------+-------
3500 | 3500
3900 | 7400
4200 | 11600
4500 | 16100
4800 | 20900
4800 | 25700
5000 | 30700
5200 | 35900
5200 | 41100
6000 | 47100

http://www.postgresql.org/docs/current/static/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS

关于postgresql - 带有 postgres 窗口函数的重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25086723/

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