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;