别名在子查询案例语句SQL中无法识别

时间:2014-06-27 12:26:54

标签: sql sql-server case where alias

我有以下查询在没有填充Where的情况下正常运行,但我很难在where中获取数据的正确引用。

代码如下:

SELECT g.Id as Id,                                                                                               
       g.Printed as Printed,
       g.CreatedDate as CreatedDate , 

       case when (                            
           Select Count(Distinct custSubGA.CustomerId) from PickingAssignment_PickingAssignmentUserGroup custSubG 
              join PickingAssignment custSubGA on custSubGA.Id = custSubG.PickingAssignmentId
                      join Customer custSub2C on custSubGA.CustomerId = custSub2C.Id
                            where custSubG.PickingAssignmentUserGroupId = Max(g.Id)
                    ) > 1 
                    then 'Multiple' 
       else (
           Select Max(custSub2C.Name) from PickingAssignment_PickingAssignmentUserGroup custSub2G 
               join PickingAssignment custSub2GA on custSub2G.PickingAssignmentId = custSub2GA.Id                               
               join Customer custSub2C on custSub2GA.CustomerId = custSub2C.Id
               where custSub2G.PickingAssignmentUserGroupId = Max(g.Id) 
       ) end  as CustomerName 

from PickingAssignmentUserGroup g  
where   CustomerName like 'exet%'  

我也尝试使用“where custSub2C.Name喜欢'exet%',但也没有用。多部分标识符custSub2C.Name无法绑定是我得到的。

这是我第一次在SQL中尝试使用case语句,所以我有可能做错了。

任何帮助都会很棒。如果您需要更多信息,请与我们联系。

2 个答案:

答案 0 :(得分:2)

您不能在同一级别的where语句中使用列别名。在大多数数据库中,您只需使用子查询:

select *
from (SELECT g.Id as Id, g.Printed as Printed, g.CreatedDate as CreatedDate , 
             case when (Select Count(Distinct custSubGA.CustomerId)
                        from PickingAssignment_PickingAssignmentUserGroup custSubG join
                             PickingAssignment custSubGA
                             on custSubGA.Id = custSubG.PickingAssignmentId join
                             Customer custSub2C
                             on custSubGA.CustomerId = custSub2C.Id
                        where custSubG.PickingAssignmentUserGroupId = Max(g.Id)
                    ) > 1 
                    then 'Multiple' 
                  else (Select Max(custSub2C.Name)
                        from PickingAssignment_PickingAssignmentUserGroup custSub2G join 
                             PickingAssignment custSub2GA
                             on custSub2G.PickingAssignmentId = custSub2GA.Id join                           
                             Customer custSub2C
                             on custSub2GA.CustomerId = custSub2C.Id
                        where custSub2G.PickingAssignmentUserGroupId = Max(g.Id) 
                       )
             end  as CustomerName
      from PickingAssignmentUserGroup g  
     ) t
where   CustomerName like 'exet%'  

答案 1 :(得分:0)

正如戈登已经指出的那样,列别名在WHERE后解析,但也可以将CASE中的子查询移动到FROM,并且作为唯一的区别是SELECT中的字段,合并它们。

SELECT g.Id as Id,
       g.Printed as Printed,
       g.CreatedDate as CreatedDate , 

       CASE WHEN CM.CustomerCount > 1 THEN 'Multiple' 
            ELSE CM.Name 
       END as CustomerName

FROM   PickingAssignmentUserGroup g
       OUTER APPLY (
             SELECT Max(custSub2C.Name) Name
                  , Count(Distinct custSubGA.CustomerId) CustomerCount
             FROM   PickingAssignment_PickingAssignmentUserGroup custSub2G 
                    INNER JOIN PickingAssignment custSub2GA 
                       ON custSub2G.PickingAssignmentId = custSub2GA.Id
                    INNER JOIN Customer custSub2C 
                       ON custSub2GA.CustomerId = custSub2C.Id
             WHERE  custSub2G.PickingAssignmentUserGroupId = Max(g.Id)) CM
WHERE  CM.Name like 'exet%' AND CM.CustomerCount = 1