解决方案: |
--建立临时表 select a.*,b.iquantity into ice_11 from ( SELECT I.cInvCode, SUM( CASE WHEN R.bISSTQc = 1 Or R.dDate < '2006-01-01' THEN (CASE WHEN R.bRdFlag = 1 THEN ISNULL(Rs.iQuantity,0) ELSE -ISNULL(Rs.iQuantity,0) END) ELSE 0 END) AS iBeginQtty FROM dbo.Inventory I LEFT OUTER JOIN dbo.ComputationUnit CU_G ON I.cSTComUnitCode =CU_G.cComUnitCode INNER JOIN dbo.RdRecords Rs ON I.cInvCode = Rs.cInvCode INNER JOIN dbo.RdRecord R ON Rs.ID = R.ID LEFT OUTER JOIN dbo.InventoryClass IC ON I.cInvCCode = IC.cInvCCode LEFT OUTER JOIN dbo.Warehouse W ON R.cWhCode = W.cWhCode LEFT OUTER JOIN dbo.ComputationUnit CU_M ON I.cComUnitCode = CU_M.cComunitCode LEFT OUTER JOIN dbo.ComputationUnit CU_A ON I.cAssComUnitCode = CU_A.cComunitCode WHERE ((R.dDate < '2006-01-01' And IsNull(R.bIsSTQc,0) = 1) Or (R.dDate >= '2006-01-01' And IsNull(R.bPUFirst,0) = 0 And IsNull(R.bIAFirst,0) = 0 And (not (R.cBusType = '假退料'and R.cVouchType = 11)) ) ) GROUP BY I.cInvCode HAVING SUM( CASE WHEN R.bISSTQc = 1 or R.dDate < '2006-01-01' THEN (CASE WH |