我有一种情况,我需要从一个表中获取“消耗的数量”,并将其应用于具有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_Consumption.QuantityConsumed需要从Pooled_Lots的行中减去“耗尽值”,其中Pool_Consumption.PoolId = Pooled_Lots.Pool。我无法弄清楚如何说出一个查询:
假设Id是主键,目标数据库是SQL 2005。
编辑:既然人都宣称我是“不给足够的信息,请关闭这个”这里比较:目前的 NO 的设置很多的Pool_Consumption从平局,需要从所有的大量其中Pool_Consumption.PoolId = Pooled_Lots.Pool,直到QuantityConsumed要么完全耗尽或我减去针对Pooled_Lots行的最后子集,其中Pool_Consumption.PoolId = Pooled_Lots.Pool
我不知道怎么解释这个。这不是一个功课问题,这不是一个虚构的“思想练习”。我需要帮助试图找出如何正确地从多行中减去QuantityConsumed!
答案 0 :(得分: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
答案 1 :(得分:1)
(基于问题的第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);