我将两个查询传递到inn
和out
别名列,我想这样做
res=(inn-out)
但它给了我一个错误
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
答案 0 :(得分:2)
尝试进出该功能只能在
之外引用 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
答案 1 :(得分: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