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.我无法弄清楚你如何陈述一个查询:
假设Id是主键,目标DB是SQL 2005.
编辑:由于人们宣称我"没有提供足够的信息,请关闭此信息"这里有更多: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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2745 次 |
| 最近记录: |