嵌套计算列 "Invalid column name" 错误(T-SQL 列别名)

Nested computed column "Invalid column name" error (T-SQL Column alias)

我创建了 3 个计算列作为别名,然后使用别名列来计算总成本。这是查询:

SELECT TOP 1000 [Id]
      ,[QuantityOfProduct]
      ,[Redundant_ProductName]
      ,[Order_Id]
      ,(CASE 
            WHEN [PriceForUnitOverride] is NULL 
                THEN [Redundant_PriceForUnit]
            ELSE
                [PriceForUnitOverride]
        END
        ) AS [FinalPriceForUnit]

      ,(CASE 
            WHEN [QuantityUnit_Override] is NULL 
                THEN [Redundant_QuantityUnit]
            ELSE
                [QuantityUnit_Override]
        END
        ) AS [FinalQuantityUnit]

      ,(CASE 
            WHEN [QuantityAtomic_Override] is NULL 
                THEN [Redundant_QuantityAtomic]
            ELSE
                [QuantityAtomic_Override]
        END
        ) AS [Final_QuantityAtomic]

         --***THIS IS WHERE THE QUERY CREATES AN ERROR***--
        ,([QuantityOfProduct]*[FinalPriceForUnit]*
  ([Final_QuantityAtomic]/[FinalQuantityUnit])) AS [Final_TotalPrice]


  FROM [dbo].[ItemInOrder]

  WHERE [IsSoftDeleted] = 0
  ORDER BY [Order_Id] 

控制台returns此错误消息:

Msg 207, Level 16, State 1, Line 55
Invalid column name 'FinalPriceForUnit'.
Msg 207, Level 16, State 1, Line 55
Invalid column name 'Final_QuantityAtomic'.
Msg 207, Level 16, State 1, Line 55
Invalid column name 'FinalQuantityUnit'.

如果我删除 "AS [Final_TotalPrice]" 别名计算列,不会出现错误,但我需要总价。我该如何解决这个问题?当达到 Final_TotalPrice 时,似乎还没有创建其他别名。

您不能在同一个 select 中使用别名。您可以做的是在子查询中找到值,然后在表达式外部使用它(或者可以在表达式中重复整个 case 语句)。另外,使用 COALESCE 代替 CASE.

select t.*,
    ([QuantityOfProduct] * [FinalPriceForUnit] * ([Final_QuantityAtomic] / [FinalQuantityUnit])) as [Final_TotalPrice]
from (
    select top 1000 [Id],
        [QuantityOfProduct],
        [Redundant_ProductName],
        [Order_Id],
        coalesce([PriceForUnitOverride], [Redundant_PriceForUnit]) as [FinalPriceForUnit],
        coalesce([QuantityUnit_Override], [Redundant_QuantityUnit]) as [FinalQuantityUnit],
        coalesce([QuantityAtomic_Override], [Redundant_QuantityAtomic]) as [Final_QuantityAtomic]
    from [dbo].[ItemInOrder]
    where [IsSoftDeleted] = 0
    order by [Order_Id]
    ) t;

您不能在同一个 select 中使用 table 别名。正常的解决方案是 CTE 或子查询。但是,SQL 服务器还提供 APPLY。 (Oracle 还支持 APPLY,其他数据库如 Postgres 支持使用 LATERAL 关键字的横向连接。)

我喜欢这个解决方案,因为您可以创建任意嵌套的表达式,而不必担心缩进:

SELECT TOP 1000 io.Id, io.QuantityOfProduct, io.Redundant_ProductName,
       io.Order_Id,
       x.FinalPriceForUnit, x.FinalQuantityUnit, x.Final_QuantityAtomic,
       (x.QuantityOfProduct * x.FinalPriceForUnit * x.Final_QuantityAtomic / x.FinalQuantityUnit
       ) as Final_TotalPrice
FROM dbo.ItemInOrder io OUTER APPLY
     (SELECT COALESCE(PriceForUnitOverride, Redundant_PriceForUnit) as FinalPriceForUnit,
             COALESCE(QuantityUnit_Override, Redundant_QuantityUnit) as FinalQuantityUnit
             COALESCE(QuantityAtomic_Override, Redundant_QuantityAtomic) as Final_QuantityAtomic
     ) x
WHERE io.IsSoftDeleted = 0
ORDER BY io.Order_Id ;

备注:

  • 我发现 [] 根本无法帮助我阅读或编写查询。
  • COALESCE() 比您的 CASE 语句简单得多。
  • 对于 COALESCE(),您可能会考虑将 COALESCE() 表达式放入最终计算中。