PostgreSQL在使用sumover

问题描述

我有一个具有ID和薪水的员工列表,我想查找按ID排序的前n名员工,累计薪水总和为x。 可以将sum()累加到()上作为累加的累加和 但是如果我使用: 凡

解决方法

您不能在wherehaving中使用窗口函数结果。使用子查询或CTE完成此操作:

with accumulate as (
  select id,salary,sum(salary) over (order by id) as cum_salary
    from employees
)
select * from accumulate
 where cum_salary < x;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...