Tenho a Seguinte tabela,
# 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 #
Preciso Que com um Stored Procedure Efectue o seguinte calculo:
nStock(RowActual)= nStock(RowAnterior) + nProduz(RowActual) - necessidade(RowActual)
Já fiz vários códigos e não consigo por isto a funcionar , alguém me pode ajudar ?
LAG
que retorna dados de uma linha anterior. Nas versões anteriores o workaround geralmente envolte dois left joins (para n *e *n - 1) usando uma função de rankeamento comoROW_NUMBER
. Veja alguns exemplos.