【问题标题】:Alias not recognised in subquery case statement SQL子查询案例语句 SQL 中无法识别的别名
【发布时间】:2023-04-01 22:24:01
【问题描述】:

当未填充 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 like 'exet%' ,但这也不起作用。多部分标识符 custSub2C.Name 无法绑定是我从中得到的。

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

任何帮助都会很棒。如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: sql sql-server case where alias


    【解决方案1】:

    您不能在同一级别的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%'  
    

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-16
        • 1970-01-01
        • 2019-10-16
        • 1970-01-01
        • 1970-01-01
        • 2012-02-08
        • 1970-01-01
        • 2017-07-17
        相关资源
        最近更新 更多