库存查询.sql 6.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 
  2. SELECT G.*, ISNULL(H.OnShipQty_Product, 0) AS OnShipQty_Product FROM
  3. (
  4. Select Product.ID_Product, Product.No_Product,Name_Product,Price_Product,IsNull(A.AvailableQty_ProductStocks,0) As AvailableQty_ProductStocks,IsNull(B.StockQty_ProductStocks,0) As StockQty_ProductStocks,IsNull(C.Quantity_NoSaleOutDepot,0) As Quantity_NoSaleOutDepot,IsNull(D.Quantity_NoSaleRtnInDepot,0) As Quantity_NoSaleRtnInDepot,IsNull(E.Quantity_NoPurchaseInDepot,0) As Quantity_NoPurchaseInDepot
  5. From Product
  6. Left Join
  7. (
  8. Select ID_Product,Sum(AvailableQty_ProductStocks) As AvailableQty_ProductStocks
  9. From ProductStocks
  10. Join Location On Location.ID_Location=ProductStocks.ID_Location
  11. Where Level_Location=0
  12. Group BY ID_Product
  13. )A On A.ID_Product=Product.ID_Product
  14. Left Join
  15. (
  16. Select ID_Product,Sum(StockQty_ProductStocks) As StockQty_ProductStocks
  17. From ProductStocks
  18. Join Location On Location.ID_Location=ProductStocks.ID_Location
  19. Where Level_Location=1
  20. Group BY ID_Product
  21. )B On B.ID_Product=Product.ID_Product
  22. Left Join
  23. (
  24. Select SaleBillProduct.ID_Product,Sum(Quantity_Sale-Quantity_OutDepot) As Quantity_NoSaleOutDepot
  25. From SaleSourceBillProduct
  26. Join SaleBillProduct On SaleSourceBillProduct.ID_SaleSourceBillProduct=SaleBillProduct.ID_SaleBillProduct
  27. Where (Quantity_Sale-Quantity_OutDepot)>0
  28. Group By SaleBillProduct.ID_Product
  29. )C On C.ID_Product=Product.ID_Product
  30. Left Join
  31. (
  32. Select SaleBillProduct.ID_Product,Sum(Quantity_SaleRtn-Quantity_InDepot) As Quantity_NoSaleRtnInDepot
  33. From SaleSourceBillProduct
  34. Join SaleBillProduct On SaleSourceBillProduct.ID_SaleSourceBillProduct=SaleBillProduct.ID_SaleBillProduct
  35. Where (Quantity_SaleRtn-Quantity_InDepot)>0
  36. Group By SaleBillProduct.ID_Product
  37. )D On D.ID_Product=Product.ID_Product
  38. Left Join
  39. (
  40. Select po.ID_Product,Sum(ps.Quantity_Order-ps.Quantity_InDepot-ps.Quantity_EndCase) As Quantity_NoPurchaseInDepot
  41. From PurchaseSourceBillProduct ps
  42. Join PurchaseOrderBillProduct po On po.ID_PurchaseOrderBillProduct=ps.ID_PurchaseSourceBillProduct
  43. Where (ps.Quantity_Order-ps.Quantity_InDepot-ps.Quantity_EndCase)>0
  44. Group BY po.ID_Product
  45. )E On E.ID_Product=Product.ID_Product
  46. Where No_Product between IsNull(@StartNo_Product,No_Product) And IsNull(@EndNo_Product,No_Product)
  47. And Name_Product between IsNull(@StartName_Product,Name_Product) And IsNull(@EndName_Product,Name_Product)
  48. And Type_Product between IsNull(@StartType_Product,Type_Product) And IsNull(@EndType_Product,Type_Product)
  49. And (A.AvailableQty_ProductStocks Is Not Null Or B.StockQty_ProductStocks Is Not Null Or C.Quantity_NoSaleOutDepot Is Not Null Or D.Quantity_NoSaleRtnInDepot Is Not Null Or E.Quantity_NoPurchaseInDepot Is Not Null)
  50. --Order By Product.Type_Product, Product.Name_Product
  51. )G
  52. left join
  53. (
  54. select ID_Product, SUM(ISNULL(Quantity_OnShipProduct, 0)) AS OnShipQty_Product from OnShipProduct
  55. GROUP BY ID_Product
  56. ) H ON G.ID_Product = H.ID_Product
  57. ,@StartNo_Product nvarchar(64),@EndNo_Product nvarchar(64),@StartType_Product nvarchar(64),@EndType_Product nvarchar(64),@StartName_Product nvarchar(64),@EndName_Product nvarchar(64),@StartNo_Product=N'12345',@EndNo_Product=NULL,@StartType_Product=NULL,@EndType_Product=NULL,@StartName_Product=NULL,@EndName_Product=NULL