【问题标题】:"AMOUNT1" is not valid in the context where it is used“AMOUNT1”在使用它的上下文中无效
【发布时间】:2016-10-01 10:35:25
【问题描述】:

我正在 DB2 中执行以下查询:

select  
  SUM (orders.totalproduct
      +orders.TOTALTAX
      +orders.totalshipping
      -orders.totaladjustment) as amount1 
from 
  orders  
where 
  amount1>10000

查询没有执行,我得到了这个异常:

“AMOUNT1”在使用它的上下文中无效。SQLCODE=-206,SQLSTATE=42703,DRIVER=3.64.96 SQL 代码:-206,SQL 状态:42703

我做错了什么?

【问题讨论】:

  • 请正确格式化您的代码。

标签: sql db2


【解决方案1】:

您不能在 DB2 中同时创建和使用 amount1。

试试这个:

 select * from (
 select SUM (orders.totalproduct+orders.TOTALTAX+orders.totalshipping- orders.totaladjustment) as amount1 from orders 
 ) tmp where amount1>10000

或者这个:

select SUM (orders.totalproduct+orders.TOTALTAX+orders.totalshipping-  orders.totaladjustment) as amount1 from orders 
having  SUM (orders.totalproduct+orders.TOTALTAX+orders.totalshipping-orders.totaladjustment)>10000

【讨论】:

    【解决方案2】:

    这个话题已经被回答了,我的第一个例子是重复的[但格式不同];我提供的东西可能有一些价值/附加值。

    分配给 SELECT 列列表中的表达式的名称在同一 SELECT 查询的每个其他子句的范围内不可参考;例如在那里分配的名称可用于 ORDER BY 子句中的引用,但不能在 WHERE 子句或 HAVING 子句中引用 - 因此,解释了看到的错误。

    为避免在 HAVING 子句中重复表达式 [注意:聚合不允许在 WHERE 子句中使用,除非将其重写为标量子选择],考虑为其中的表达式结果指定一个名称派生表表达式;这可以将名称范围限定到该派生表的查询中。通过消除对 duplicate 表达式的引用,如果需要对查询进行任何修订,这也可以避免必须保持两个副本相同。

    此处显示的表标识符和限定列名的使用都是可选的,但包括在内是为了清楚引用名称的来源;示例显示了两种编写派生表以引用命名表达式的方法。

    select S.AMOUNT1
    from table /* Nested Table Expression (NTE) */
      ( select 
          SUM ( orders.totalproduct
              + orders.TOTALTAX
              + orders.totalshipping
              - orders.totaladjustment
              ) as amount1 
        from orders
      ) as S
    where S.amount1>10000
    
    with /* Common Table Expression (CTE) */
      aggSum ( AMOUNT1 ) as
        ( select 
             SUM ( orders.totalproduct
                 + orders.TOTALTAX
                 + orders.totalshipping
                 - orders.totaladjustment
                 ) as amount1 /* named here; or, as shown, named above */
          from orders
        )
    select C.AMOUNT1
    from aggSum as C /* from the above (CTE) */
    where C.amount1>10000
    

    虽然也有以下选项 [我怀疑我是否会编写代码,因为],但我发现这比在 HAVING 子句中重复引用表达式更难阅读 [即如已接受答案中的第二个示例所示]。此查询将相同的聚合查询封装在子查询中,然后在 WHERE 子句中将其作为 标量子选择 引用:

      select 
        SUM ( orders.totalproduct
            + orders.TOTALTAX
            + orders.totalshipping
            - orders.totaladjustment
            ) as amount1 
      from orders
      where ( select 
                SUM ( orders.totalproduct
                    + orders.TOTALTAX
                    + orders.totalshipping
                    - orders.totaladjustment
                    )
              from orders
            ) > 10000
    

    【讨论】:

      【解决方案3】:

      在 db2 中,您不能在 where/have 的同一个子查询中使用您为列创建的别名。

      即使在 MySQL/BigQuery 中,我认为您也只能引用 group by/order by/have 语句中的任何别名,而不是 where。

      使用子查询并在那里过滤,或复制列的代码(不带别名)并将其粘贴到 where。但会推荐子查询选项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多