• <tfoot id='dv5P9'></tfoot>
  • <i id='dv5P9'><tr id='dv5P9'><dt id='dv5P9'><q id='dv5P9'><span id='dv5P9'><b id='dv5P9'><form id='dv5P9'><ins id='dv5P9'></ins><ul id='dv5P9'></ul><sub id='dv5P9'></sub></form><legend id='dv5P9'></legend><bdo id='dv5P9'><pre id='dv5P9'><center id='dv5P9'></center></pre></bdo></b><th id='dv5P9'></th></span></q></dt></tr></i><div id='dv5P9'><tfoot id='dv5P9'></tfoot><dl id='dv5P9'><fieldset id='dv5P9'></fieldset></dl></div>

    • <bdo id='dv5P9'></bdo><ul id='dv5P9'></ul>
  • <legend id='dv5P9'><style id='dv5P9'><dir id='dv5P9'><q id='dv5P9'></q></dir></style></legend>

      <small id='dv5P9'></small><noframes id='dv5P9'>

        嵌套计算列“无效的列名"错误(T-SQL 列别名

        时间:2023-06-06

        <i id='YXaAA'><tr id='YXaAA'><dt id='YXaAA'><q id='YXaAA'><span id='YXaAA'><b id='YXaAA'><form id='YXaAA'><ins id='YXaAA'></ins><ul id='YXaAA'></ul><sub id='YXaAA'></sub></form><legend id='YXaAA'></legend><bdo id='YXaAA'><pre id='YXaAA'><center id='YXaAA'></center></pre></bdo></b><th id='YXaAA'></th></span></q></dt></tr></i><div id='YXaAA'><tfoot id='YXaAA'></tfoot><dl id='YXaAA'><fieldset id='YXaAA'></fieldset></dl></div>
        • <bdo id='YXaAA'></bdo><ul id='YXaAA'></ul>
              <tbody id='YXaAA'></tbody>
            <legend id='YXaAA'><style id='YXaAA'><dir id='YXaAA'><q id='YXaAA'></q></dir></style></legend>

              <small id='YXaAA'></small><noframes id='YXaAA'>

              • <tfoot id='YXaAA'></tfoot>

                1. 本文介绍了嵌套计算列“无效的列名"错误(T-SQL 列别名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

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

                  I've create 3 computed columns as alias and then used the aliased columns to calculate the total cost. This is the query:

                  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] 
                  

                  控制台返回此错误消息:

                  The console returns this ERROR message:

                  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 时还没有创建其他别名.

                  If I remove the "AS [Final_TotalPrice]" alias computed column, no error occurs, but I need the total price. How can I solve this issue? It seems as the other aliases have not been created when the Final_TotalPrice is reached.

                  推荐答案

                  不能在同一个选择中使用表别名.正常的解决方案是 CTE 或子查询.但是,SQL Server 也提供 APPLY.(Oracle 还支持 APPLY,其他数据库(例如 Postgres)支持使用 LATERAL 关键字进行横向连接.)

                  You can't use table aliases in the same select. The normal solution is CTEs or subqueries. But, SQL Server also offers APPLY. (Oracle also supports APPLY and other databases such as Postgres support lateral joins using the LATERAL keyword.)

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

                  I like this solution, because you can create arbitrarily nested expressions and don't have to worry about indenting:

                  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() 表达式放入最终计算中.
                  • I don't find that [ and ] help me read or write queries at all.
                  • COALESCE() is much simpler than your CASE statements.
                  • With COALESCE() you might consider just putting the COALESCE() expression in the final calculation.

                  这篇关于嵌套计算列“无效的列名"错误(T-SQL 列别名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:SQL 函数 - 使用 Levenshtein 距离算法进行模糊匹配 下一篇:如何从 SQL Server 中的查询中获取列名

                  相关文章

                  最新文章

                  <small id='3Vzuf'></small><noframes id='3Vzuf'>

                    <tfoot id='3Vzuf'></tfoot>
                      <bdo id='3Vzuf'></bdo><ul id='3Vzuf'></ul>

                    <i id='3Vzuf'><tr id='3Vzuf'><dt id='3Vzuf'><q id='3Vzuf'><span id='3Vzuf'><b id='3Vzuf'><form id='3Vzuf'><ins id='3Vzuf'></ins><ul id='3Vzuf'></ul><sub id='3Vzuf'></sub></form><legend id='3Vzuf'></legend><bdo id='3Vzuf'><pre id='3Vzuf'><center id='3Vzuf'></center></pre></bdo></b><th id='3Vzuf'></th></span></q></dt></tr></i><div id='3Vzuf'><tfoot id='3Vzuf'></tfoot><dl id='3Vzuf'><fieldset id='3Vzuf'></fieldset></dl></div>

                      <legend id='3Vzuf'><style id='3Vzuf'><dir id='3Vzuf'><q id='3Vzuf'></q></dir></style></legend>