根据 SQL 值的最新更改按日期/ID 运行总计
我有一个特殊的情况,我想计算每天的运行总量。我已经搜索了很多但找不到正确的答案。在代码方面,我没有什么可以分享的,因为它涉及很多敏感数据
下面是虚拟数据表:
如您所见,按日期排列有多个重复的 ID。我希望能够计算日期的运行总计,如下所示:
对于 2022/03/24,运行总计将为 9+33 = 42,在 2022/03/26,运行总计应为 9+ 31 = 40。本质上,任何给定日期的运行总计应按 ID 选择最后一个值(如果发生更改)或存在的值。在本例中,该日期为 2022 年 3 月 26 日,对于 ID 2072,我们选择 31 而不是 33,因为这是可用的最新值。
预期输出:
可能跨越很多天,并且运行总计需要逐日进行。
可能的相关问题:SQL Server running Total基于列状态的更改
PS:对于上下文,ID 只是项目库存的唯一标识符。每个项目的数量每天都在变化。在此示例中,ID 1 的 inventory 上次更改时间为 2022 年 3 月 24 日,而 ID 2072 则更改了多次。 2022 年 3 月 24 日的运行总计为当天的库存商品数量。 26 日,ID 1 没有任何变化,但 ID 2072 发生了变化,库存池应反映总计为 ID 2072 的当前库存大小+ ID 1 的当前大小。26 日,ID 1 再次没有任何变化,但 ID 2072改变了。因此,库存大小 = ID 2072 的当前大小 + ID 1 的当前大小,在本例中为 40。本质上,它只是当前的库存大小,每天都会发生变化。
如有任何帮助,我们将不胜感激!谢谢。
I have a unique case where I want to calculate the running total of quantities day over day. I have been searching a lot but couldn't find the right answer. Code-wise, there is nothing much I can share as it refers to a lot of sensitive data
Below is the table of dummy data:
As you can see, there are multiple duplicate IDs by date. I want to be able to calculate the running total of a date as follows:
For 2022/03/24, the running total would be 9+33 = 42, on 2022/03/26 the running total should be 9+31 = 40. Essentially, the running total for any given day should pick the last value by ID if it changed or the value that exists. In this case on 2022/03/26 for that date, for ID 2072, we pick 31 and not 33 because that's the latest value available.
Expected Output:
There maybe be many days spanning across and the running total needs to be day over day.
Possible related question: SQL Server running total based on change of state of a column
PS: For context, ID is just a unique identifier for an inventory of items. Each item's quantity changes day by day. In this example, ID 1's inventoyr last changed on 2022/03/24 where as ID 2072's changed multiple times. Running total for 2022/03/24 would be quantities of inventory items on that day. On 26th there are no changes for ID 1 but ID 2072 changed, the inventory pool should reflect the total as current inventory size of ID 2072+ current size of ID 1. On 26th, again ID 1 did not have any change, but ID 2072 changed. Therefore inventory size = current size of ID 2072 + current size of ID 1, in this case, 40. Essentially, it is just a current size of inventory with day over day change.
Any help would be really appreciated! Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我添加了更多行,以防万一这是您真正想要的。
我用的是T-SQL。
结果:
运行质量日期
2022年3月1日 10
2022年3月24日 52
2022年3月25日 62
2022年3月26日 60
2022年3月27日 70
I added a few more rows just in case if this is what you really wanted.
I used T-SQL.
Result:
dates running_quality
2022-03-01 10
2022-03-24 52
2022-03-25 62
2022-03-26 60
2022-03-27 70