2013-05-14 89 views
-1

我通過兩個查詢到innout別名列,我想這樣做, res=(inn-out),但它給我一個錯誤。減去2別名列

Invalid column name 'inn'. 
Invalid column name 'out'. 
? 

select productname 
,inn=isnull((select sum(orderqty) 
       from purchasing.purchaseinvoicedetail 
       where productfk=Production.product.productid),0) 
,outt=isnull((select sum(orderqty) 
       from sales.salesinvoicedetail 
       where productfk=Production.product.productid ),0) 
,res=(inn-outt) 
from production.product 

回答

2

試試這個inn和outt只能在外面引用

Select a.*,inn-outt As [DiffValue] from 
    (
    select productname,inn=isnull((select sum(orderqty) from purchasing.purchaseinvoicedetail where productfk=Production.product.productid),0), 
     outt=isnull((select sum(orderqty) from sales.salesinvoicedetail where productfk=Production.product.productid ),0) 
     from production.product 

     ) As a 
+0

謝謝你很好地工作 – 2013-05-14 06:06:56

0

試試這個:

SELECT t.*, t.Inn - t.outt AS Res 
FROM 
(
select productname 
,inn=isnull((select sum(orderqty) 
       from purchasing.purchaseinvoicedetail 
       where productfk=Production.product.productid),0) 
,outt=isnull((select sum(orderqty) 
       from sales.salesinvoicedetail 
       where productfk=Production.product.productid ),0) 
from production.product 
) t 
+0

任何問題或只是一個隨機downvote? – hims056 2013-05-14 06:09:50