SQL – 从行中减去耗尽的值

我有一种情况,我需要从一个表中获取“数量消耗”,并将其应用于具有一个或多个“批量”数量的行的第二个表。 我不确定如何更好地描述它,下面是我从表格的angular度来看:

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_Consumption.QuantityConsumed需要是从Pooled_Lots中的行中减去的“耗尽值”,其中Pool_Consumption.PoolId = Pooled_Lots.Pool。 我无法弄清楚你将如何陈述一个查询说:

  • 如果不在最后一行,AmtConsumedFromLot = Quantity – QuantityConsumed如果QuantityConsumed <数量,否则数量
  • 如果有更多的行,QuantityConsumed = QuantityConsumed – Quantity
  • 循环直到最后一行
  • 如果最后一行,AmtConsumedFromLot = QuantityConsumed

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

编辑:由于人们宣称我“没有提供足够的信息,请closures这个”这里是更多的: 没有设置很多,Pool_Consumption绘制,它需要从Pool_Consumption.PoolId = Pooled_Lots.Pool 所有地方绘制,直到QuantityConsumed要么完全耗尽,要么减去Pooled_Lots行的最后一个子集,其中Pool_Consumption.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 

(基于问题的第4版,因为我的WiFi已经停机了一段时间)

 (SELECT Pool, SUM(Quantity) as Pool_Quantity FROM Pooled_Lots GROUP BY Pool) as Pool_Quantity_Table 

现在你有一个池数量汇总到一个单一的值。

现在完整的查询:

 SELECT Pool_Consumption.PoolID as Pool, Pool_Quantity_Table.Pool_Quantity as Quantity, Pool_Consumption.QuantityConsumed as AmtConsumedFromLot, (Pool_Quantity_Table.Pool_Quantity - Pool_Consumption.QuantityConsumed) as SurplusOrDefecit FROM Pool_Consumption INNER JOIN (SELECT Pool, SUM(Quantity) as Pool_Quantity FROM Pooled_Lots GROUP BY Pool) as Pool_Quantity_Table ON (Pool_Consumption.PoolID = Pool_Quantity_Table.Pool);