首先看一下这个sql语句,然后以下是查询结果
语句如下:
select
e.ProductName,
e.Productid,
e.ProductNum,
e.Warehousename,
e.Warehouseid,
e.CheckInNum,
e.CheckOutNum,
e.StockNum from (select
d.ProductName,
d.Productid,
d.ProductNum,
d.Warehousename,
d.Warehouseid,
sum(d.CheckInNum) AS CheckInNum,
sum(d.CheckOutNum) AS CheckOutNum ,
sum(d.StockNum) AS StockNum from (SELECT
a.WarehouseID, a.Warehouse AS WarehouseName,
a.ProductID, a.ProductName,a.ProductNum,ISNULL(b.ConfirmNum, 0) AS CheckInNum,
ISNULL(c.ConfirmNum, 0) AS CheckOutNum, ISNULL(b.ConfirmNum, 0) - ISNULL(c.ConfirmNum, 0) AS StockNum
FROM (SELECT
wh.ID AS WarehouseID, wh.Name AS Warehouse,
pro.ID AS ProductID,
pro.Name AS ProductName,
pro.Num AS ProductNum
FROM
dbo.Warehouse AS wh
CROSS JOIN
dbo.Product AS pro
WHERE
(wh.sc = 0) AND (pro.sc = 0)) AS a
LEFT OUTER JOIN (SELECT
ProductName,
ProductNameid,
Productcode AS ProductNum,
Warehouse,
Warehouseid,
SUM(ConfirmNum) AS ConfirmNum
FROM dbo.CheckInConfirmList
WHERE (sc = 0)
GROUP BY
ProductNameid, ProductName, Warehouseid, Warehouse,Productcode) AS b
ON a.WarehouseID = b.Warehouseid AND a.ProductID = b.ProductNameid
LEFT OUTER JOIN (SELECT
ProductName,
ProductNameid,
Productcode AS ProductNum,
Warehouse,
Warehouseid,
SUM(ConfirmNum) AS ConfirmNum
FROM dbo.CheckOutConfirmList
WHERE (sc = 0)
GROUP BY
ProductNameid, ProductName, Warehouseid, Warehouse,Productcode) AS c
ON
a.WarehouseID = c.Warehouseid AND a.ProductID = c.ProductNameid) d group by
d.ProductName,d.Productid,d.ProductNum,d.Warehousename,d.Warehouseid) e
where e.CheckInNum!='0' or e.CheckOutNum!='0' or e.StockNum!='0'