-1

是否不能使用“as [item] 然后在查询中使用 item 变量。

例如:

select c.category as [category],c.orderby as [CatOrder], m.masterno, m.master
,-- select OUT (select count(*) from    rentalitem       ri  with (nolock), 
            rentalitemstatus ris with (nolock), 
            rentalstatus     rs  with (nolock)
    where   ri.rentalitemid    = ris.rentalitemid
            and   ris.rentalstatusid = rs.rentalstatusid
            and   ri.masterid        = m.masterid
            and   rs.statustype     in ('OUT', 'INTRANSIT', 'ONTRUCK'))  as [qtyout] 
,-- select OWNED owned=
(select top 1 mwq.qty                                           
    from    masterwhqty mwq                                              
    where   mwq.masterid    = m.masterid) 

, -([owned]-[qtyout]) as [Variance]

from master m 
    inner join category c on c.categoryid=m.categoryid and c.categoryid=@category
    inner join inventorydepartment d on c.inventorydepartment=@department

在计算方差时,我似乎无法使用 qtyout 或拥有。我怎样才能做到这一点?

4

3 回答 3

2

您还可以使用表变量,然后像您在上面尝试做的那样引用该表变量......这是MSDN中的一个示例

USE AdventureWorks2012;
GO
DECLARE @MyTableVar table(
    EmpID int NOT NULL,
    OldVacationHours int,
    NewVacationHours int,
    ModifiedDate datetime);
UPDATE TOP (10) HumanResources.Employee
SET VacationHours = VacationHours * 1.25,
    ModifiedDate = GETDATE() 
OUTPUT inserted.BusinessEntityID,
       deleted.VacationHours,
       inserted.VacationHours,
       inserted.ModifiedDate
INTO @MyTableVar;
--Display the result set of the table variable.
SELECT EmpID, OldVacationHours, NewVacationHours, ModifiedDate
FROM @MyTableVar;
GO
--Display the result set of the table.
SELECT TOP (10) BusinessEntityID, VacationHours, ModifiedDate
FROM HumanResources.Employee;
GO
于 2013-04-02T21:06:28.827 回答
1

需要将您的计算字段移动到子查询中,然后在外部查询中通过它们的别名使用它们。

select subquery.*, -([owned]-[qtyout]) as [Variance]
from
(
    select c.category as [category],c.orderby as [CatOrder], m.masterno, m.master
    ,-- select OUT (select count(*) from    rentalitem       ri  with (nolock), 
                rentalitemstatus ris with (nolock), 
                rentalstatus     rs  with (nolock)
        where   ri.rentalitemid    = ris.rentalitemid
                and   ris.rentalstatusid = rs.rentalstatusid
                and   ri.masterid        = m.masterid
                and   rs.statustype     in ('OUT', 'INTRANSIT', 'ONTRUCK'))  as [qtyout] 
    ,-- select OWNED owned=
    (select top 1 mwq.qty                                           
        from    masterwhqty mwq                                              
        where   mwq.masterid    = m.masterid) as [owned]

    from master m 
        inner join category c on c.categoryid=m.categoryid and c.categoryid=@category
        inner join inventorydepartment d on c.inventorydepartment=@department
) as subquery
于 2013-04-02T20:29:06.150 回答
0

您需要使用子查询:

select t.*,
       ([owned]-[qtyout]) as [Variance]
from (<something like your query here
     ) t

您查询,即使没有评论,也不太有意义(select OUT (select . . .例如)。但是,您的问题的答案是在子查询或 CTE 中定义基本变量,然后随后使用它们。

而且,您将差异称为“差异”。正如您所知,您正在重新定义该术语的统计含义(http://en.wikipedia.org/wiki/Variance),它基于差异的平方。

于 2013-04-02T20:29:14.507 回答