请大神帮看一下SQL语句嵌套的问题。谢谢大神。
SELECT rtrim(IBC.ITEM_NO),rtrim(IBI.ITEM_NAME),SUPCUST.SUP_NAME,CO.CODE_NAME,INSTO.INSTOQTY,SALE.SALEQTY,STO.STOQTY
FROM T_BD_ITEM_BARCODE AS IBC
LEFT JOIN T_BD_ITEM_INFO AS IBI ON IBC.ITEM_NO=IBI.ITEM_NO
LEFT JOIN T_BD_BASE_CODE AS CO ON IBC.COLOR_CODE = CO.CODE_ID AND CO.TYPE_NO='CO'
LEFT JOIN (SELECT BAR_QTY.ITEM_BARCODE, SUM(CASE PSM.TRANS_NO WHEN 'PI' THEN BAR_QTY.QTY ELSE 0 END)-SUM(CASE PSM.TRANS_NO WHEN 'RO' THEN BAR_QTY.QTY ELSE 0 END) AS INSTOQTY FROM T_PM_SHEET_DETAIL AS PSD,T_PM_SHEET_MASTER AS PSM,T_IM_SHEET_BARCODE_QTY AS BAR_QTY,T_BD_ITEM_BARCODE AS BBC WHERE PSD.SHEET_NO=PSM.SHEET_NO AND PSM.TRANS_NO IN('PI','RO','GI') AND PSM.APPROVE_FLAG='1' AND PSD.SHEET_NO=BAR_QTY.SHEET_NO AND BAR_QTY.ITEM_NO=PSD.ITEM_NO AND BAR_QTY.ITEM_BARCODE = BBC.ITEM_BARCODE GROUP BY BAR_QTY.ITEM_BARCODE) AS INSTO ON IBC.ITEM_BARCODE=INSTO.ITEM_BARCODE
LEFT JOIN (SELECT ITEM_BARCODE,SUM(SALE_QTY-RET_QTY+GIV_QTY)AS SALEQTY FROM T_RM_DAYSUM WHERE OPER_DATE BETWEEN '2012-07-11' AND '2012-08-31' AND BRANCH_NO ='0101' GROUP BY ITEM_BARCODE) AS SALE ON IBC.ITEM_BARCODE=SALE.ITEM_BARCODE
LEFT JOIN (SELECT ITEM_BARCODE,SUM(STOCK_QTY) AS STOQTY FROM T_IM_BARCODE_STOCK GROUP BY ITEM_BARCODE) AS STO ON IBC.ITEM_BARCODE=STO.ITEM_BARCODE
LEFT JOIN T_BD_SUPCUST_INFO AS SUPCUST ON IBI.MAIN_SUPCUST = SUPCUST.SUPCUST_NO
left join
(Select *
FROM OPENROWSET('SQLOLEDB','192.168.1.3';'sa';'pwd-128',
'SELECT rtrim(IBC.ITEM_NO),rtrim(IBI.ITEM_NAME),SUPCUST.SUP_NAME,CO.CODE_NAME,SUM(INSTO.INSTOQTY),SUM(SALE.SALEQTY),SUM(STO.STOQTY)
FROM HBFSV8.DBO.T_BD_ITEM_BARCODE AS IBC
LEFT JOIN HBFSV8.DBO.T_BD_ITEM_INFO AS IBI ON IBC.ITEM_NO=IBI.ITEM_NO
LEFT JOIN HBFSV8.DBO.T_BD_BASE_CODE AS CO ON IBC.COLOR_CODE = CO.CODE_ID AND CO.TYPE_NO=''CO''
LEFT JOIN (SELECT BAR_QTY.ITEM_BARCODE, SUM(CASE PSM.TRANS_NO WHEN ''PI'' THEN BAR_QTY.QTY ELSE 0 END)-SUM(CASE PSM.TRANS_NO WHEN ''RO'' THEN BAR_QTY.QTY ELSE 0 END) AS INSTOQTY FROM HBFSV8.DBO.T_PM_SHEET_DETAIL AS PSD,HBFSV8.DBO.T_PM_SHEET_MASTER AS PSM,HBFSV8.DBO.T_IM_SHEET_BARCODE_QTY AS BAR_QTY,HBFSV8.DBO.T_BD_ITEM_BARCODE AS BBC WHERE PSD.SHEET_NO=PSM.SHEET_NO AND PSM.TRANS_NO IN(''PI'',''RO'',''GI'') AND PSM.APPROVE_FLAG=''1'' AND PSD.SHEET_NO=BAR_QTY.SHEET_NO AND BAR_QTY.ITEM_NO=PSD.ITEM_NO AND BAR_QTY.ITEM_BARCODE = BBC.ITEM_BARCODE GROUP BY BAR_QTY.ITEM_BARCODE) AS INSTO ON IBC.ITEM_BARCODE=INSTO.ITEM_BARCODE
LEFT JOIN (SELECT ITEM_BARCODE,SUM(SALE_QTY-RET_QTY+GIV_QTY)AS SALEQTY FROM HBFSV8.DBO.T_RM_DAYSUM WHERE OPER_DATE BETWEEN ''2012-07-11'' AND ''2012-08-31'' GROUP BY ITEM_BARCODE) AS SALE ON IBC.ITEM_BARCODE=SALE.ITEM_BARCODE
LEFT JOIN (SELECT ITEM_BARCODE,SUM(STOCK_QTY) AS STOQTY FROM HBFSV8.DBO.T_IM_BARCODE_STOCK GROUP BY ITEM_BARCODE) AS STO ON IBC.ITEM_BARCODE=STO.ITEM_BARCODE
LEFT JOIN HBFSV8.DBO.T_BD_SUPCUST_INFO AS SUPCUST ON IBI.MAIN_SUPCUST = SUPCUST.SUPCUST_NO WHERE IBC.ITEM_NO LIKE ''52%'' AND IBI.MAIN_SUPCUST =''127''AND IBI.STATUS=1
GROUP BY IBC.ITEM_NO,IBI.ITEM_NAME,SUPCUST.SUP_NAME,CO.CODE_NAME
ORDER BY IBC.ITEM_NO,CO.CODE_NAME '))
WHERE IBC.ITEM_NO LIKE '52%' AND IBI.MAIN_SUPCUST ='01'AND IBI.STATUS=1
------解决方案--------------------
SQL code
') as a)
as b on 关联条件
WHERE