gpt4 book ai didi

SQL - 从行中减去消耗值

转载 作者:行者123 更新时间:2023-12-02 18:56:32 25 4
gpt4 key购买 nike

我遇到一种情况,我需要从一个表中获取“消耗的数量”,并将其应用于第二个表,该表有 1 行或多行是“汇集批处理”的数量。我不知道如何更好地描述它,这就是我从表格角度的意思:

Table Pooled_Lots
----------------------------
Id Pool Lot Quantity
1 1 1 5
2 1 2 10
3 1 3 4
4 2 1 7
5 3 1 1
6 3 2 5

Table Pool_Consumption
----------------------------
Id PoolId QuantityConsumed
1 1 17
2 2 8
3 3 10

我需要一个来自 SQL 查询的结果行集,如下所示:

Pool    Lot Quantity    QuantityConsumed    RunningQuantity RemainingDemand SurplusOrDeficit
1 1 5 17 0 12 NULL
1 2 10 17 0 2 NULL
1 3 4 17 2 0 2
2 1 7 8 0 1 -1
3 1 1 10 0 9 NULL
3 2 5 10 0 4 -4

因此,Pool_Conclusion.QuantityConsumed 需要是从 Pooled_Lots 中的行中减去的“消耗值”,其中 Pool_Conclusion.PoolId = Pooled_Lots.Pool。我不知道你会如何提出这样的查询:

  • 如果不在最后一行,AmtConsumedFromLot = Quantity - QuantityConsumed if QuantityConsumed < Quantity, else Quantity
  • 如果有更多行,则 QuantityConsumed = QuantityConsumed - Quantity
  • 循环直到最后一行
  • 如果是最后一行,AmtConsumedFromLot = QuantityConsumed

假设Id是主键,目标数据库是SQL 2005。

编辑:由于人们宣称我“没有提供足够的信息,请关闭此内容”这里还有更多内容:Pool_Conclusion 没有从中提取的没有设置批处理,它需要从中提取>所有 地 block ,其中 Pool_Conclusion.PoolId = Pooled_Lots.Pool,直到 QuantityConsumed 完全耗尽,或者我正在减去 Pooled_Lots 行的最后一个子集,其中 Pool_Conclusion.PoolId = Pooled_Lots.Pool

我不知道如何进一步解释这一点。这不是一个家庭作业问题,这不是一个编造的“思维练习”。我需要帮助尝试找出如何正确减去多行的 QuantityConsumed!

最佳答案

留给OP的练习:根据示例数据计算出正确的结果并总结以下查询的结果:

-- Create some test data.
declare @Pooled_Lots as table ( Id int, Pool int, Lot int, Quantity int );
insert into @Pooled_Lots ( Id, Pool, Lot, Quantity ) values
( 1, 1, 1, 5 ), ( 2, 1, 2, 10 ), ( 3, 1, 3, 4 ),
( 4, 2, 1, 7 ),
( 5, 3, 1, 1 ), ( 6, 3, 2, 5 );
declare @Pool_Consumption as table ( Id int, Pool int, QuantityConsumed int );
insert into @Pool_Consumption ( Id, Pool, QuantityConsumed ) values
( 1, 1, 17 ), ( 2, 2, 8 ), ( 3, 3, 10 );

select * from @Pooled_Lots order by Pool, Lot;
select * from @Pool_Consumption order by Pool;

with Amos as (
-- Start with Lot 1 for each Pool.
select PL.Pool, PL.Lot, PL.Quantity, PC.QuantityConsumed,
case
when PC.QuantityConsumed is NULL then PL.Quantity
when PL.Quantity >= PC.QuantityConsumed then PL.Quantity - PC.QuantityConsumed
when PL.Quantity < PC.QuantityConsumed then 0
end as RunningQuantity,
case
when PC.QuantityConsumed is NULL then 0
when PL.Quantity >= PC.QuantityConsumed then 0
when PL.Quantity < PC.QuantityConsumed then PC.QuantityConsumed - PL.Quantity
end as RemainingDemand
from @Pooled_Lots as PL left outer join
@Pool_Consumption as PC on PC.Pool = PL.Pool
where Lot = 1
union all
-- Add the next Lot for each Pool.
select PL.Pool, PL.Lot, PL.Quantity, CTE.QuantityConsumed,
case
when CTE.RunningQuantity + PL.Quantity >= CTE.RemainingDemand then CTE.RunningQuantity + PL.Quantity - CTE.RemainingDemand
when CTE.RunningQuantity + PL.Quantity < CTE.RemainingDemand then 0
end,
case
when CTE.RunningQuantity + PL.Quantity >= CTE.RemainingDemand then 0
when CTE.RunningQuantity + PL.Quantity < CTE.RemainingDemand then CTE.RemainingDemand - CTE.RunningQuantity - PL.Quantity
end
from Amos as CTE inner join
@Pooled_Lots as PL on PL.Pool = CTE.Pool and PL.Lot = CTE.Lot + 1
)
select *,
case
when Lot = ( select max( Lot ) from @Pooled_Lots where Pool = Amos.Pool ) then RunningQuantity - RemainingDemand
else NULL end as SurplusOrDeficit
from Amos
order by Pool, Lot;

关于SQL - 从行中减去消耗值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9420173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com