【问题标题】:Calculating Running Total with OVER Clause and PARTITION BY Clause with counter使用 OVER 子句和 PARTITION BY 子句与计数器计算运行总计
【发布时间】:2019-02-04 14:27:47
【问题描述】:

如何使用计数器计算每个 week_number 和 user_name 的总和? 我用sum() OVER (PARTITION BY ORDER BY )

我希望最大总和为 10。
我该怎么做?

这是我的原始表格:

这是我想要得到的结果:

这里是sql代码:

SELECT week_number,
user_name,
sum(points) OVER (PARTITION BY user_name ORDER BY week_number) AS total_prime
FROM team;

【问题讨论】:

    标签: sql postgresql sum window-functions


    【解决方案1】:

    试试这个

    select 
      week_number, 
      user_name, 
      points, 
      case when total_prime > 10 then 10 else total_prime end as sum, 
      case when total_prime > 1 and total_prime < 10 then null 
           when total_prime > 10 then total_prime - 10 end as dif
    from
      (select 
         week_number, 
         user_name, points, 
         sum(points) over (partition by user_name order by week_number) as total_prime 
       from 
         team) a
    

    我没有在你的第二张桌子上找到它,alanpoint 4 但有sum 2

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2021-12-01
      • 2019-08-13
      • 2014-03-10
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多