简体   繁体   English

使用前一行计算剩余总数的 SQL 查询

[英]SQL query to calculate remaining total using previous row

I'm trying to write a query that will calculate remaining total points for each day when some points have been completed.我正在尝试编写一个查询,该查询将在完成某些点后计算每天剩余的总点数。

Story points completed in a day一天完成的故事点

So if my total is 100 points, it should show 99, 97.5, 94.5 as I want to use it in a burndown graph in Power BI.因此,如果我的总分是 100 分,它应该显示 99、97.5、94.5,因为我想在 Power BI 的燃尽图中使用它。

If I use LAG, it will calculate the remaining for each row separately, instead of using 'updated' total from previous row.如果我使用 LAG,它将分别计算每一行的剩余部分,而不是使用前一行的“更新”总数。

Also - for rest of the days with no change in points it should show most recent remaining total.另外 - 在剩下的日子里,积分没有变化,它应该显示最近的剩余总数。

Any ideas?有任何想法吗?

You can use SUM() OVER window function:您可以使用 SUM() OVER 窗口函数:

With MyTbl as (
select *
from (values
   ('2022-06-08', 1.0)
  ,('2022-06-13', 1.5)
  ,('2022-06-14', 3.0)
     ) T(Created, StoryPoints)
 ) 
select 
   M.*, 
   RemainingPoints=100-SUM(StoryPoints) over (order by Created rows between unbounded preceding and current row)
from MyTbl M

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM