数据库复杂查询,多表多联

首先看一下这个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'

解释:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值