在SQL Server中减去两个别名列

时间:2013-05-14 05:56:37

标签: sql sql-server-2008 alias

我将两个查询传递到innout别名列,我想这样做 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

2 个答案:

答案 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