问题
I'm executing the the following query in DB2:
select
SUM (orders.totalproduct
+orders.TOTALTAX
+orders.totalshipping
-orders.totaladjustment) as amount1
from
orders
where
amount1>10000
The query is not executed, I get this exception instead:
"AMOUNT1" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.64.96 SQL Code: -206, SQL State: 42703
What am I doing wrong?
回答1:
You can't create and use amount1 in same time in DB2.
Try this:
select * from (
select SUM (orders.totalproduct+orders.TOTALTAX+orders.totalshipping- orders.totaladjustment) as amount1 from orders
) tmp where amount1>10000
or this:
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:
This topic was since answered, and my first example is a duplicate [but formatted differently]; there may be something of value/add in what I offer.
The name assigned to an expression on the SELECT column-list is not available for reference within the scope of every other clause of the same SELECT query; e.g. the name assigned there, is available for reference in an ORDER BY clause, but can not be reference in either a WHERE clause or HAVING clause -- thus, explains the error seen.
To avoid repeating the expression in the HAVING clause [noting: the aggregate is not allowed in a WHERE clause unless rewritten as a scalar subselect], consider assigning a name to the result of the expression within a derived table expression; that enables scoping of the name into the query of that derived table. By eliminating reference to a duplicate expression, that also prevents having to maintain the two copies identically, if any revisions need to be made to the query.
The table identifier and use of qualified column name shown here, are both optional, but included to be clear about whence the referenced name comes; examples show two ways to code a derived table for referencing the named expression.
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
Although there is the following option as well [one that I doubt I would ever code, because], I find this much more difficult to read than having just made duplicate references to the expression in the HAVING clause [i.e. as shown in the second example in the accepted answer]. This query encapsulates the same aggregate query in a subquery as that is then referenced as a scalar subselect on the WHERE clause:
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:
in db2, you can't use an alias you've created for your columns in the same sub query for the where/having.
Even in MySQL/BigQuery, I think you can only reference any aliases in the group by/order by/having statements, not the where.
Use a sub query and filter there, or copy the code for the column (WITHOUT the alias) and paste it in the where. But would recommend the sub query option.
来源:https://stackoverflow.com/questions/39805576/amount1-is-not-valid-in-the-context-where-it-is-used