插入列直到达到某个值(基于FIFO的插入)

时间:2016-10-05 09:30:59

标签: sql sql-server sql-server-2008

我正在使用ms server 2008,并且不知道如何设计基于FIFO的插入特定列,直到达到一定量的其他列。

基本上我想要的只是按发货日期/项目收货日期订购列,填写“收到的货物价值”栏,直到达到“价格”栏中的值,并在“货物”栏中插入剩余价值剩下的”。所以我知道还剩下多少。有可能吗?

例如,我有一张带有货物的表格(ID,价格,发货日期,发货日期)。我想要做的是,我想按goods_id和发货日期订购此列表。我有第二个表格存档所有收到的货物。第二个表有货物ID,货物价值和收到物品的日期。

所以我基本上想要的是描述以下示例

商品表:

|goods id|price|shipment date from| shipment date to|goods value remaining | goods value received|
     1       100   2016-09-26         2016-10-30         NULL                             NULL
     1       200   2016-09-22         2016-10-30         NULL                             NULL
     1       50    2016-09-23         2016-10-30         NULL                             NULL
     2       500   2016-05-10         2016-09-30         NULL                             NULL
     2       20    2016-05-12         2016-09-30         NULL                             NULL
     2       300   2016-05-04         2016-09-30         NULL                             NULL
     3       400   2016-10-12         2016-11-30         NULL                             NULL
     3       300   2016-10-06         2016-11-30         NULL                             NULL 


Goods received Table

|goods id|item_value|item_received_date|

     1        20   2016-09-26       
     1       200   2016-09-22              
     2       500   2016-05-10         
     2       20    2016-05-12        
     2       400   2016-05-04         
     3       400   2016-10-12        
     3       100   2016-10-06        



INSERT into Goods Table with FIFO System:

First step:
order by shipment date:
|goods id|price|shipment date from| shipment date to|goods value remaining | goods value received|
     2       300   2016-05-04         2016-09-30            NULL              NULL
     2       500   2016-05-10         2016-09-30            NULL              NULL
     2       20    2016-05-12         2016-09-30            NULL              NULL
     1       200   2016-09-22         2016-10-30            NULL              NULL
     1       50    2016-09-23         2016-10-30            NULL              NULL
     1       100   2016-09-26         2016-10-30            NULL              NULL
     3       300   2016-10-06         2016-11-30            NULL              NULL 
     3       400   2016-10-12         2016-11-30            NULL              NULL

Second step:
Order the received goods table by "item received date".

     1       200   2016-09-22  
     1        20   2016-09-26  
     2       400   2016-05-04   
     2       500   2016-05-10         
     2       20    2016-05-12       
     3       400   2016-10-06      
     3       100   2016-10-12        



Insert into column (goods value received) with FIFO System until "price" is reached. If the price has rechead, begin to insert into next row until the price of the column has reached:

example:

   |goods id|price|shipment date from| shipment date to| goods value remaining |goods value received|
     2       300   2016-05-04         2016-09-30                                     300    -- 100 left (insert the 100 on the next row and add the remaining value)
     2       500   2016-05-10         2016-09-30                                     500    -- added 100 and insert 400 of 500. 100 are left
     2       20    2016-05-12         2016-09-30                                     20     -- added 20. 80 from last row are left + 20 are left from the received table. finished
     1       200   2016-09-22         2016-10-30                                     200    -- added 200 of the received table. 
     1       50    2016-09-23         2016-10-30                                     20     -- the last 20 added of the received table. 
     1       100   2016-09-26         2016-10-30             130                     0      -- 130 are remaining! This value I need to know!
     3       300   2016-10-06         2016-11-30                                     300    -- added 300 of 400. 100 remaining.
     3       400   2016-10-12         2016-11-30             200                     200    -- added 100 of the last row, added 100 of the received item table. 200 are left. this is the value I need to know.

如果你能帮助我,我真的很感激,因为我不知道如何编码。

谢谢你, 吉克斯

1 个答案:

答案 0 :(得分:0)

您需要为您的需求创建存储过程。

为货物(比如说C1)创建一个游标,然后为C1.goods_id创建一个游标,用于创建接收货物的游标(比如C2)。

创建两个循环。光标C1的循环是外部的,光标C2的循环是嵌套的。 C2的循环始终为C1.goods_id运行。

在嵌套循环过程中收到货物记录并根据您的要求更新C1.goods_value_received。