根据 SQL 值的最新更改按日期/ID 运行总计

发布于 2025-01-17 04:23:05 字数 1169 浏览 3 评论 0原文

我有一个特殊的情况,我想计算每天的运行总量。我已经搜索了很多但找不到正确的答案。在代码方面,我没有什么可以分享的,因为它涉及很多敏感数据

下面是虚拟数据表:

Data

如您所见,按日期排列有多个重复的 ID。我希望能够计算日期的运行总计,如下所示:

对于 2022/03/24,运行总计将为 9+33 = 42,在 2022/03/26,运行总计应为 9+ 31 = 40。本质上,任何给定日期的运行总计应按 ID 选择最后一个值(如果发生更改)或存在的值。在本例中,该日期为 2022 年 3 月 26 日,对于 ID 2072,我们选择 31 而不是 33,因为这是可用的最新值。

预期输出:

预期OP

可能跨越很多天,并且运行总计需要逐日进行。

可能的相关问题: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:

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:

Expected OP

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜血染红嫁衣 2025-01-24 04:23:05

我添加了更多行,以防万一这是您真正想要的。

我用的是T-SQL。

declare @orig table(
id          int,
quantity    int,
rundate     date
)


insert into @orig
values (1,9,'20220324'),(2072,33,'20220324'),(2072,31,'20220326'),(2072,31,'20220327'),
(2,10,'20220301'),(2,20,'20220325'),(2,30,'20220327')


declare @dates table (
runningdate date
)

insert into @dates
select distinct rundate from @orig
order by rundate


declare @result table (
dates               date,
running_quality     int
)


DECLARE @mydate date
DECLARE @sum int

-- CURSOR definition
DECLARE my_cursor CURSOR FOR  
SELECT * FROM @dates

OPEN my_cursor

-- Perform the first fetch
FETCH NEXT FROM my_cursor into @mydate

-- Check @@FETCH_STATUS to see if there are any more rows to fetch
WHILE @@FETCH_STATUS = 0
BEGIN

;with cte as (
select * from @orig
where rundate <= @mydate
), cte2 as (
select id, max(rundate) as maxrundate
from cte
group by id
), cte3 as (
select a.*
from cte as a join cte2 as b
on a.id = b.id and a.rundate = b.maxrundate
)
select @sum = sum(quantity)
from cte3

insert into @result
select @mydate, @sum


-- This is executed as long as the previous fetch succeeds
FETCH NEXT FROM my_cursor into @mydate

END -- cursor

CLOSE my_cursor
DEALLOCATE my_cursor

select * from @result

结果:

运行质量日期
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.

declare @orig table(
id          int,
quantity    int,
rundate     date
)


insert into @orig
values (1,9,'20220324'),(2072,33,'20220324'),(2072,31,'20220326'),(2072,31,'20220327'),
(2,10,'20220301'),(2,20,'20220325'),(2,30,'20220327')


declare @dates table (
runningdate date
)

insert into @dates
select distinct rundate from @orig
order by rundate


declare @result table (
dates               date,
running_quality     int
)


DECLARE @mydate date
DECLARE @sum int

-- CURSOR definition
DECLARE my_cursor CURSOR FOR  
SELECT * FROM @dates

OPEN my_cursor

-- Perform the first fetch
FETCH NEXT FROM my_cursor into @mydate

-- Check @@FETCH_STATUS to see if there are any more rows to fetch
WHILE @@FETCH_STATUS = 0
BEGIN

;with cte as (
select * from @orig
where rundate <= @mydate
), cte2 as (
select id, max(rundate) as maxrundate
from cte
group by id
), cte3 as (
select a.*
from cte as a join cte2 as b
on a.id = b.id and a.rundate = b.maxrundate
)
select @sum = sum(quantity)
from cte3

insert into @result
select @mydate, @sum


-- This is executed as long as the previous fetch succeeds
FETCH NEXT FROM my_cursor into @mydate

END -- cursor

CLOSE my_cursor
DEALLOCATE my_cursor

select * from @result

Result:

dates running_quality
2022-03-01 10
2022-03-24 52
2022-03-25 62
2022-03-26 60
2022-03-27 70

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文