2
I have the following table,
# id # dataTmp # referencia #nProduz#nStock # id2 #necessi#
# 115237 # 31-01-2017 # VL03280103 # 0 # 0 # 6 # 0 #
# 115238 # 01-02-2017 # VL03280103 # 0 # 0 # 7 # 0 #
# 115239 # 01-02-2017 # VL03280103 # 0 # 0 # 8 # 0 #
# 115240 # 01-02-2017 # VL03280103 # 0 # 0 # 9 # 0 #
# 115241 # 02-02-2017 # VL03280103 # 0 # 0 # 10 # 0 #
# 115242 # 02-02-2017 # VL03280103 # 0 # 0 # 11 # 0 #
# 115243 # 02-02-2017 # VL03280103 # 0 # 0 # 12 # 0 #
# 115244 # 03-02-2017 # VL03280103 # 0 # 0 # 13 # 0 #
# 115245 # 03-02-2017 # VL03280103 # 0 # 0 # 14 # 0 #
# 115246 # 03-02-2017 # VL03280103 # 0 # 0 # 15 # 0 #
# 115247 # 04-02-2017 # VL03280103 # 0 # 0 # 16 # 0 #
# 115248 # 04-02-2017 # VL03280103 # 0 # 0 # 17 # 0 #
# 115249 # 04-02-2017 # VL03280103 # 0 # 0 # 18 # 0 #
# 115250 # 05-02-2017 # VL03280103 # 0 # 0 # 19 # 0 #
# 115251 # 05-02-2017 # VL03280103 # 0 # 0 # 20 # 0 #
# 115252 # 05-02-2017 # VL03280103 # 0 # 0 # 21 # 0 #
# 115253 # 06-02-2017 # VL03280103 # 0 # 0 # 22 # 0 #
# 115254 # 06-02-2017 # VL03280103 # 0 # 0 # 23 # 0 #
# 115255 # 06-02-2017 # VL03280103 # 0 # 0 # 24 # 0 #
# 115256 # 07-02-2017 # VL03280103 # 0 # 0 # 25 # 0 #
# 115257 # 07-02-2017 # VL03280103 # 0 # 0 # 26 # 0 #
# 115258 # 07-02-2017 # VL03280103 # 0 # 0 # 27 # 0 #
# 115259 # 08-02-2017 # VL03280103 # 0 # 0 # 28 # 0 #
# 115260 # 08-02-2017 # VL03280103 # 0 # 0 # 29 # 0 #
# 115261 # 08-02-2017 # VL03280103 # 0 # 0 # 30 # 0 #
# 115262 # 30-01-2017 # VL03280203 # 0 # 7290 # 1 # 0 #
# 115263 # 30-01-2017 # VL03280203 # 0 # 7290 # 2 # 0 #
# 115264 # 30-01-2017 # VL03280203 # 0 # 7290 # 3 # 0 #
# 115265 # 31-01-2017 # VL03280203 # 0 # 0 # 4 # 0 #
# 115266 # 31-01-2017 # VL03280203 # 0 # 0 # 5 # 0 #
# 115267 # 31-01-2017 # VL03280203 # 0 # 0 # 6 # 0 #
# 115268 # 01-02-2017 # VL03280203 # 0 # 0 # 7 # 0 #
# 115269 # 01-02-2017 # VL03280203 # 0 # 0 # 8 # 0 #
# 115270 # 01-02-2017 # VL03280203 # 0 # 0 # 9 # 0 #
# 115271 # 02-02-2017 # VL03280203 # 0 # 0 # 10 # 0 #
# 115272 # 02-02-2017 # VL03280203 # 0 # 0 # 11 # 0 #
# 115273 # 02-02-2017 # VL03280203 # 0 # 0 # 12 # 0 #
# 115274 # 03-02-2017 # VL03280203 # 0 # 0 # 13 # 0 #
# 115275 # 03-02-2017 # VL03280203 # 0 # 0 # 14 # 0 #
# 115276 # 03-02-2017 # VL03280203 # 0 # 0 # 15 # 0 #
# 115277 # 04-02-2017 # VL03280203 # 0 # 0 # 16 # 0 #
# 115278 # 04-02-2017 # VL03280203 # 0 # 0 # 17 # 0 #
# 115279 # 04-02-2017 # VL03280203 # 0 # 0 # 18 # 0 #
# 115280 # 05-02-2017 # VL03280203 # 0 # 0 # 19 # 0 #
# 115281 # 05-02-2017 # VL03280203 # 0 # 0 # 20 # 0 #
# 115282 # 05-02-2017 # VL03280203 # 0 # 0 # 21 # 0 #
# 115283 # 06-02-2017 # VL03280203 # 0 # 0 # 22 # 0 #
# 115284 # 06-02-2017 # VL03280203 # 0 # 0 # 23 # 0 #
# 115285 # 06-02-2017 # VL03280203 # 0 # 0 # 24 # 0 #
# 115286 # 07-02-2017 # VL03280203 # 0 # 0 # 25 # 0 #
# 115287 # 07-02-2017 # VL03280203 # 0 # 0 # 26 # 0 #
# 115288 # 07-02-2017 # VL03280203 # 0 # 0 # 27 # 0 #
# 115289 # 08-02-2017 # VL03280203 # 0 # 0 # 28 # 0 #
# 115290 # 08-02-2017 # VL03280203 # 0 # 0 # 29 # 0 #
# 115291 # 08-02-2017 # VL03280203 # 0 # 0 # 30 # 0 #
I need a Stored Procedure Perform the following calculation:
nStock(RowActual)= nStock(RowAnterior) + nProduz(RowActual) - necessidade(RowActual)
I’ve done several codes and I can’t get it to work, someone can help me ?
How will the case of the first line, since there is no previous line?
– Reginaldo Rigo
It must always be executed starting with Monday ... never with the first ...
– Bruno Almeida
In order to calculate the value of the current line and a previous line, you will need to use a cursor, already tried this way?
– Ricardo Pontual
I’ve tried, but I haven’t, yet :)
– Bruno Almeida
Which version of SQL Server are you using? In 2012 the function was introduced
LAG
which returns data from a previous line. In previous versions the workaround usually wrapped two left joins (for *n *and n - 1) using a ranking function such asROW_NUMBER
. Behold some examples.– Anthony Accioly
That ID will always be in order and without voids as the example?
– Reginaldo Rigo
You want your stored Procedure to calculate all at once or you will pass the current line id and it will only update this?
– Sorack
"ID2" is always incremented according to the reference , while "ID" is auto-increment
– Bruno Almeida