1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| SELECT
v.[Numero]
,v.[Dates]
,v.[IdAgence]
,[IdAgcom]
,[Client]
,[cni_client]
,v.[Idproduit]
,v.[Categorie]
,[numero_serie]
,a.[Quantite] as Entree
,v.[Quantite] as Sortie
,s.[Quantite] as Report
,SUM(COALESCE(a.[Quantite],0)+COALESCE(s.[Quantite],0)-COALESCE(v.[Quantite],0)) OVER (PARTITION BY v.IdAgence,v.Idproduit ORDER BY v.[Dates],a.[Quantite],s.[Quantite],v.[Quantite] ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS STOCK_INITIAL
,SUM(COALESCE(a.[Quantite],0)+COALESCE(s.[Quantite],0)-COALESCE(v.[Quantite],0)) OVER (PARTITION BY v.IdAgence,v.Idproduit ORDER BY v.[Dates],a.[Quantite],s.[Quantite],v.[Quantite]) AS ETAT_STOCK
,SUM(COALESCE(a.[Quantite],0)+COALESCE(s.[Quantite],0)-COALESCE(v.[Quantite],0)) OVER (PARTITION BY v.IdAgence,v.Idproduit ORDER BY v.[Dates],a.[Quantite],s.[Quantite],v.[Quantite]) AS STOCK_FINAL
from [Ventes] v
left outer join Stockinitiale s on s.[IdAgence]=v.IdAgence and s.Idproduit=v.Idproduit
left outer join [Achats] a on a.IdAgence=v.IdAgence and a.Idproduit=v.Idproduit |
Partager