【问题标题】:Postgresql selecting specific rows when using sum() over((Postgresql 在使用 sum() over((
【发布时间】:2021-01-08 17:41:49
【问题描述】:

我有一个有 id 和薪水的员工列表,我想找到按 id 排序的前 n 个员工,累计薪水总和为 x。 可以用 sum() over () 将累积和相加为 cumsum 但如果我使用: 其中 cumsum

【问题讨论】:

  • edit您的问题(通过点击下面的edit链接)并添加一些示例数据和基于该数据的预期输出为formatted text .请参阅here,了解有关如何创建漂亮的文本表格的一些提示。 (edit 您的问题 - 在 cmets 中邮政编码或其他信息)

标签: postgresql cumulative-sum


【解决方案1】:

您不能在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;

【讨论】:

    猜你喜欢
    • 2020-06-29
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多