Jim.Mueksch@wellsfargo.com wrote:
Quote:
I am having a problem with using calculated values in a WHERE clause.
My query is below. DB2 gives me this error message: Error: SQL0206N
"APPRAISAL_LESS_PRICE" is not valid in the context where it is used.
SQLSTATE=42703
>
SELECT DISTINCT S3.OPR_APPLICATION_NR,
S3.APPLICATION_ID,
S3.APPRAISAL_TYPE_CD,
S3.Appraisal_Used_Amount,
S3.RPT_LEVEL2_NR,
S3.PROP_CLTRL_PRCHS_LAST_6_MO_IN,
S3.Price,
S3.Appraisal_Value,
(S3.Appraisal_Used_Amount - S3.Price) AS
value_Used_Less_Price,
(S3.Appraisal_Value - S3.Price) AS Appraisal_Less_Price,
(S3.Appraisal_Value - S3.Appraisal_Used_Amount) AS
APPRAISAL_VAL_MINUS_VAL_USED
FROM LM_PURCHASE_STEP3 AS S3
WHERE (Appraisal_Less_Price >= 0) AND (VALUE_USED_LESS_PRICE <= 0)
OR (Appraisal_Less_Price <= 0) AND (APPRAISAL_VAL_MINUS_VAL_USED)
>
>
What am I doing wrong?
Have a look here:
http://publib.boulder.ibm.com/infoce...n/r0000875.htm
As you can see, the WHERE clause is evaluated _before_ the select list.
Therefore, you cannot use values calculated in the select list in the where
clause.
This also makes sense especially if you consider functions with side effects
(i.e. external action) being applied in the SELECT list. You only want to
call such functions for qualifying only and not for all rows.
An alternative may be to use common table expressions or subselects:
SELECT ...
FROM ( SELECT col1, col2 + col3 AS value
FROM ... ) AS t
WHERE value BETWEEN ... AND ...
--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany