SQL - 从行中减去耗尽值

Iri*_*can 6 sql t-sql sql-server sql-server-2005

我有一种情况,我需要从一个表中获取"消耗的数量",并将其应用于具有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
Run Code Online (Sandbox Code Playgroud)

我需要一个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
Run Code Online (Sandbox Code Playgroud)

因此,Pool_Consumption.QuantityConsumed需要从Pooled_Lots的行中减去"耗尽值",其中Pool_Consumption.PoolId = Pooled_Lots.Pool.我无法弄清楚你如何陈述一个查询:

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

假设Id是主键,目标DB是SQL 2005.

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

我不知道怎么解释这个.这不是一个功课问题,这不是一个虚构的"思想练习".我需要帮助试图找出如何正确地从多行中减去QuantityConsumed!

HAB*_*ABO 7

作为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
Run Code Online (Sandbox Code Playgroud)

  • +1用于确定问题所在 (2认同)
  • @TimLarson - 看起来像是典型的仓库选股问题.我需要13个星大小的Sneetches.抓住星空Sneetch架子上的第一个盒子,看看是否足够了.如果是这样,很好.如果没有,请抓住下一个框.当你从架子的末端掉下来时,你会抓住你的油漆和模板,然后开始寻找没有星星的Sneetches. (2认同)