查询,子查询和使用子查询中的变量

时间:2013-04-02 20:19:10

标签: sql sql-server sql-server-2008 reporting-services

是否无法使用“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或拥有。我怎么能这样做?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:0)

你需要使用子查询:

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

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

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