

CREATE VIEW [dbo].[ApiWmsStockRealView] as
 select ItemId, LocationAdress, LocationId,  RepositoryId, s.ItemFeatures,s.Barcode,  s.TS
 , Quanity - ISNULL(( select SUM(OrgQuantity) from dbo.ApiWmsDocLines l JOIN dbo.ApiWmsDocuments d ON d.Id = l.DocumentId and d.StateId=0  and d.Direction<0 where l.ItemId = s.ItemId and d.RepositoryId = s.RepositoryId and l.LocationId = s.LocationId and l.ItemFeatures = s.ItemFeatures),0) SQuantity
  from dbo.ApiWmsStocks s
GO


