【问题标题】:Query, subquery and using as variables from subquery查询、子查询并用作子查询中的变量
【发布时间】:2013-04-02 20:19:10
【问题描述】:

是不是可以使用"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 或 own。我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    您也可以使用表变量,然后像上面尝试的那样引用该表变量....这是来自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
    

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

        【解决方案3】:

        你需要使用子查询:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多