循环使用sql server更新每日库存余额

时间:2016-09-09 02:58:19

标签: sql-server

我有以下字段(part_code,stock_date,beginning_stock,stock_in,stock_out,stock_balance)的示例数据: part_code stock_date beginning_stock stock_in stock_out stock_balance

part_code   stock_date  beginning_stock stock_in    stock_out   stock_balance
A   1/1/2013    79  0   0   79
B   1/1/2013    3   0   0   3
C   1/1/2013    93  0   0   93
D   1/1/2013    47  0   0   47
E   1/1/2013    77  0   0   77
F   1/1/2013    34  0   0   34
G   1/1/2013    143 0   0   143
C   1/2/2013    0   0   -27 0
G   1/2/2013    0   0   -1  0
A   1/3/2013    0   0   -4  0
C   1/3/2013    0   0   -6  0
D   1/3/2013    0   0   -5  0
E   1/3/2013    0   0   -4  0
G   1/3/2013    0   0   -3  0
C   1/4/2013    0   0   -11 0
D   1/4/2013    0   0   -14 0
E   1/4/2013    0   0   -3  0
G   1/4/2013    0   9   -1  0
A   1/5/2013    0   0   -4  0
D   1/5/2013    0   0   -1  0
E   1/5/2013    0   0   -5  0
G   1/5/2013    0   76  0   0
A   1/8/2013    0   0   -3  0
C   1/8/2013    0   0   -12 0
D   1/8/2013    0   0   -4  0
E   1/8/2013    0   0   -7  0
G   1/8/2013    0   14  -3  0
A   1/9/2013    0   0   -4  0
B   1/9/2013    0   26  0   0
C   1/9/2013    0   0   -8  0
D   1/9/2013    0   0   -4  0
E   1/9/2013    0   0   -4  0
G   1/9/2013    0   0   -13 0
A   1/10/2013   0   0   -6  0
B   1/10/2013   0   24  0   0
C   1/10/2013   0   0   -9  0
D   1/10/2013   0   0   -7  0
E   1/10/2013   0   0   -4  0
G   1/10/2013   0   1   -16 0

我希望有一个循环来通过part_code更新每天的beginning_stock和stock_balance,如下所示:

part_code   stock_date  beginning_stock stock_in    stock_out   stock_balance
A   1/1/2013    79  0   0   79
B   1/1/2013    3   0   0   3
C   1/1/2013    93  0   0   93
D   1/1/2013    47  0   0   47
E   1/1/2013    77  0   0   77
F   1/1/2013    34  0   0   34
G   1/1/2013    143 0   0   143
C   1/2/2013    93  0   -27 66
G   1/2/2013    143 0   -1  142
A   1/3/2013    79  0   -4  75
C   1/3/2013    66  0   -6  60
D   1/3/2013    47  0   -5  42
E   1/3/2013    77  0   -4  73
G   1/3/2013    142 0   -3  139
C   1/4/2013    60  0   -11 49
D   1/4/2013    42  0   -14 28
E   1/4/2013    73  0   -3  70
G   1/4/2013    139 9   -1  147
A   1/5/2013    75  0   -4  71
D   1/5/2013    28  0   -1  27
E   1/5/2013    70  0   -5  65
G   1/5/2013    147 76  0   223
A   1/8/2013    71  0   -3  68
C   1/8/2013    49  0   -12 37
D   1/8/2013    27  0   -4  23
E   1/8/2013    65  0   -7  58
G   1/8/2013    223 14  -3  234
A   1/9/2013    68  0   -4  64
B   1/9/2013    3   26  0   29
C   1/9/2013    37  0   -8  29
D   1/9/2013    23  0   -4  19
E   1/9/2013    58  0   -4  54
G   1/9/2013    234 0   -13 221
A   1/10/2013   64  0   -6  58
B   1/10/2013   29  24  0   53
C   1/10/2013   29  0   -9  20
D   1/10/2013   19  0   -7  12
E   1/10/2013   54  0   -4  50
G   1/10/2013   221 1   -16 206

第一天的 stock_balance 将在第二天变为 beginning_stock balance_stock = beginning_stock + stock_in + stock_out

任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

这会给你预期的结果:

CREATE TABLE #TABLE(part_code VARCHAR(5), stock_date DATE, beginning_stock INT,stock_in INT,   stock_out  INT, stock_balance INT)

INSERT INTO #TABLE(part_code, stock_date, beginning_stock, stock_in, stock_out, stock_balance)
VALUES
('A','1/1/2013',    79  ,0   ,0   ,79),
('B','1/1/2013',    3   ,0   ,0   ,3 ),
('C','1/1/2013',    93  ,0   ,0   ,93),
('D','1/1/2013',    47  ,0   ,0   ,47),
('E','1/1/2013',    77  ,0   ,0   ,77),
('F','1/1/2013',    34  ,0   ,0   ,34),
('G','1/1/2013',    143 ,0   ,0   ,14),
('C','1/2/2013',    0   ,0   ,-27 ,0 ),
('G','1/2/2013',    0   ,0   ,-1  ,0 ),
('A','1/3/2013',    0   ,0   ,-4  ,0 ),
('C','1/3/2013',    0   ,0   ,-6  ,0 ),
('D','1/3/2013',    0   ,0   ,-5  ,0 ),
('E','1/3/2013',    0   ,0   ,-4  ,0 ),
('G','1/3/2013',    0   ,0   ,-3  ,0 ),
('C','1/4/2013',    0   ,0   ,-11 ,0 ),
('D','1/4/2013',    0   ,0   ,-14 ,0 ),
('E','1/4/2013',    0   ,0   ,-3  ,0 ),
('G','1/4/2013',    0   ,9   ,-1  ,0 ),
('A','1/5/2013',    0   ,0   ,-4  ,0 ),
('D','1/5/2013',    0   ,0   ,-1  ,0 ),
('E','1/5/2013',    0   ,0   ,-5  ,0 ),
('G','1/5/2013',    0   ,76  ,0   ,0 ),
('A','1/8/2013',    0   ,0   ,-3  ,0 ),
('C','1/8/2013',    0   ,0   ,-12 ,0 ),
('D','1/8/2013',    0   ,0   ,-4  ,0 ),
('E','1/8/2013',    0   ,0   ,-7  ,0 ),
('G','1/8/2013',    0   ,14  ,-3  ,0 ),
('A','1/9/2013',    0   ,0   ,-4  ,0 ),
('B','1/9/2013',    0   ,26  ,0   ,0 ),
('C','1/9/2013',    0   ,0   ,-8  ,0 ),
('D','1/9/2013',    0   ,0   ,-4  ,0 ),
('E','1/9/2013',    0   ,0   ,-4  ,0 ),
('G','1/9/2013',    0   ,0   ,-13 ,0 ),
('A','1/10/2013',   0   ,0   ,-6  ,0 ),
('B','1/10/2013',   0   ,24  ,0   ,0 ),
('C','1/10/2013',   0   ,0   ,-9  ,0 ),
('D','1/10/2013',   0   ,0   ,-7  ,0 ),
('E','1/10/2013',   0   ,0   ,-4  ,0 ),
('G','1/10/2013',   0   ,1   ,-16 ,0 );

SELECT T1.part_code,stock_date,
COALESCE
(
    (SELECT SUM(t2.beginning_stock + T2.stock_in + T2.stock_out)  FROM #TABLE T2 WHERE T2.part_code = T1.part_code AND T2.stock_date < T1.stock_date ) ,
    beginning_stock
)AS beginning_stock ,
stock_in, stock_out,
(SELECT ISNULL(SUM(t2.beginning_stock + T2.stock_in + T2.stock_out),0) FROM #TABLE T2 WHERE T2.part_code = T1.part_code AND T2.stock_date <= T1.stock_date ) AS stock_balance 
FROM #TABLE T1
ORDER BY T1.stock_date,T1.part_code