我对更新声明感到担忧。 我的表的主键由以下列组成:
ReferencePiece, NumeroPhase, AncienPoste,DateStrategie, DateCadence, NomProgramme, VersionProgramme.
这是我的程序:
update S
set S.stock=T.Stock-T.NombrePiecesParAvion*T.CadenceProgramme
from SITUATIONS_Final_1 S inner join SITUATIONS_Final_1 T
on S.ReferencePiece=T.ReferencePiece and S.NumeroPhase=T.NumeroPhase and S.AncienPoste=T.AncienPoste
and T.DateDebProduction<S.DateDebProduction and T.stock = (select min(T.stock) from SITUATIONS_Final_1 T)
where S.DateDebProduction=(select min(S.datedebproduction) as 'date1'
from SITUATIONS_Final_1 S inner join SITUATIONS_Final_1 T
on S.ReferencePiece=T.ReferencePiece and S.NumeroPhase=T.NumeroPhase and S.AncienPoste=T.AncienPoste
where S.datedebproduction>T.DateDebProduction and T.stock = (select min(T.stock) from SITUATIONS_Final_1 T))
但实际上,由于我的更新声明中没有“分组依据”,它只考虑我所有引用的最小值(库存)并对未来日期进行更新。
我尝试按照以下方式进行,但我确信它比这更容易:
update S
set S.stock=T.Stock-T.NombrePiecesParAvion*T.CadenceProgramme
from SITUATIONS_Final_1 S inner join SITUATIONS_Final_1 T
on S.ReferencePiece=T.ReferencePiece and S.NumeroPhase=T.NumeroPhase and S.AncienPoste=T.AncienPoste
and T.DateDebProduction<S.DateDebProduction inner join (select referencepiece,NumeroPhase,AncienPoste,DateStrategie,
nomprogramme,versionprogramme, datedebproduction, min(stock) StockMini from SITUATIONS_Final_1 group by ReferencePiece,NumeroPhase,AncienPoste,DateStrategie,
nomprogramme,versionprogramme,DateDebProduction)
F on F.ReferencePiece=T.ReferencePiece and T.Stock=F.StockMini
inner join (select referencepiece,NumeroPhase,AncienPoste,DateStrategie,
nomprogramme,versionprogramme, min(datedebproduction) datemini,stock
from SITUATIONS_Final_1 group by ReferencePiece,NumeroPhase,AncienPoste,DateStrategie,
nomprogramme,versionprogramme,DateDebProduction,stock) Z on F.ReferencePiece=Z.ReferencePiece
where T.DateDebProduction=Z.datemini and Z.datemini=F.DateDebProduction and Z.Stock=F.StockMini and T.ReferencePiece=Z.ReferencePiece and S.datedebproduction>T.DateDebProduction
and T.stock=F.StockMini and S.ReferencePiece=T.ReferencePiece and T.ReferencePiece=F.ReferencePiece and F.numerophase=Z.numerophase
and T.numerophase=Z.numerophase and F.AncienPoste=Z.AncienPoste and T.AncienPoste=Z.AncienPoste and F.DateStrategie=Z.DateStrategie and T.DateStrategie =Z.DateStrategie
and F.nomprogramme=Z.nomprogramme and T.nomprogramme=Z.nomprogramme and F.versionprogramme=Z.versionprogramme and T.versionprogramme=Z.versionprogramme
and S.ReferencePiece=T.ReferencePiece and S.NumeroPhase=T.NumeroPhase and S.AncienPoste=T.AncienPoste and S.nomprogramme=T.nomprogramme
and S.versionprogramme=T.versionprogramme and S.DateStrategie=T.datestrategie
但它表现不佳:它更新相同的行(相同的日期)。我需要这个程序来做这样的事情:
Referencepiece=RefA DateDebProduction='02/05/2015' Stock=4 NbrePieces=2 CadenceProgramme=1
对于相同的参考和下一个日期库存应该是= 4-2 * 1 = 2 ... 实际上,这个程序包含在另一个程序中。它启动它5次,以便我可以确定它将更新我的所有行。如果你可以帮助我找到一种更新所有行的更好方法而不需要多次启动我的程序,那就太棒了。
以下是数据样本:
ReferencePiece NumeroPhase AncienPoste DateStrategie Charge NomProgramme VersionProgramme CadenceProgramme DateCadence DateDebProduction NombrePiecesParAvion Stock
RefA001 10 114179A 02/05/2015 0.000 Z540 A BGR 29.000 01/03/2015 01/03/2015 1.000 23.000
RefA001 10 114179A 02/05/2015 0.000 Z540 A BGR 25.000 01/04/2015 01/04/2015 1.000 NULL
RefA001 10 114179A 02/05/2015 0.000 Z540 A BGR 19.000 01/05/2015 01/05/2015 1.000 NULL
RefA001 10 114179A 02/05/2015 0.000 Z540 A BGR 25.000 01/06/2015 01/06/2015 1.000 NULL
RefXY001 10 1MR704 02/05/2015 0.220 X220 RM 2.000 01/05/2015 01/05/2015 2.000 12.000
RefXY001 10 1MR704 02/05/2015 0.220 X220 RM 2.000 01/06/2015 01/06/2015 2.000 8.000
RefXY001 10 1MR704 02/05/2015 0.220 X220 RM 2.000 01/07/2015 01/07/2015 2.000 NULL
RefXY001 10 1MR704 02/05/2015 0.220 X220 RM 3.000 01/08/2015 01/08/2015 2.000 NULL
正如您在此示例中所注意到的(我已经启动了我的程序一次),它更新了参考'RefXY001'的第二行,其中stock = 12-2 * 2 = 8,因为12 <23。 当我运行此程序时,它将仅更新参考'RefXY001'的未来日期,因为最小的库存是它。