2012-02-23 67 views
5

我有一種情況,需要從一個表中獲取「消耗的數量」,並將其應用於第二個具有「批量」數量的一行或多行的表。我不知道如何更好地形容它,這是我從一個表的角度是指:SQL - 從行中減去耗盡值

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 =數量 - QuantityConsumed如果QuantityConsumed <數量,否則數量
  • 如果更多的行,QuantityConsumed = QuantityConsumed - 產品數量
  • 循環直到最後一行
  • 如果最後一行,AmtConsumedFromLot = QuantityConsumed

假設Id是主鍵,和所述目標DB是SQL 2 005

編輯:既然人都宣稱我是「不給足夠的信息,請關閉這個」這裏比較:有NO集很多的Pool_Consumption而來,它需要從所有大量吸取借鑑其中Pool_Consumption.PoolId = Pooled_Lots.Pool,直到QuantityConsumed要麼完全耗盡或我對減去行Pooled_Lots的最後一個子集,其中Pool_Consumption.PoolId = Pooled_Lots.Pool

我不知道該怎麼越解釋這一點。這不是一個家庭作業問題,這不是一個虛構的「思考練習」。我需要幫助,試圖找出如何正確減去QuantityConsumed對多行!

+1

我不知道爲什麼現在的人了投票權這個問題,你有一些嚴重的數據粒度的問題。 「Pool_Consumption」表不指定消耗單位的來源。另外,我修正了一個錯字。 -1 – JohnB 2012-02-23 20:00:41

+0

沒關係,我不打算去修復所有的拼寫錯誤。至少,花更多的時間在這個問題上直接獲取數據!但是,您的設計有缺陷。 (投票結束) – JohnB 2012-02-23 20:04:28

+1

@JohnB這是我目前所面臨的問題,儘可能簡化並且不會泄露我工作中的機密數據。與其驕傲地宣稱我在問一個不值得投票的愚蠢問題,或許你可以啓發我在哪裏需要添加「粒度」以使我自己能夠更容易地產生期望的輸出? – Irinotecan 2012-02-23 20:05:48

回答

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 
+0

謝謝!這正是我想要的,甚至更多!(儘管我想我會包含RunningQuantity和RemainingDemand,因爲它們很有用) – Irinotecan 2012-02-23 20:54:45

+2

+1,用於確定問題的出處 – CodeThug 2012-02-23 21:08:49

+2

@TimLarson - 它看起來像一個典型的倉庫選股問題,我需要13個星期天的Sneetches。抓住星盤式Sneetch擱架上的第一個盒子,看看是否夠用。如果是這樣,很好。如果沒有,抓住下一個框。當你從架子上掉下來時,你會抓住你的油漆和模板,開始尋找沒有星星的Sneetches。 – HABO 2012-02-23 21:16:08

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); 
+0

謝謝你花時間回答這個問題,JohnB。儘管在當前情況下將結果拆分爲更多幫助,但此答案確實有助於我理解我的問題的解決方案。我對原始最終結果表中的不準確性表示歉意,我認爲我已經仔細檢查過,並且在您進行修改之前沒有看到它。 – Irinotecan 2012-02-23 21:10:23

+0

非常歡迎。我很高興你能解決你的問題! :) – JohnB 2012-02-23 21:52:11