Re: Can I use an alias name is a where clause

From: Serge Rielau <srielau_at_ca.ibm.com>
Date: Wed, 09 Mar 2005 18:28:53 -0500
Message-ID: <399f4lF5t9ssiU1_at_individual.net>


Lig wrote:
> The following select statement yeilds an error message ORA-00904:
> "Total": invalid identifier.
>
> select t.a + t.b + t.c Total
> from some_table t
> where Total > 0
>
> Why can't the alias Total be used in the Where clause?
The values in the select list do not "exist" before the where clause is executed.
The order of execution semantically is:
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY Think about this:
[Quoted] SELECT x/y as z FROM T WHERE y IS NOT NULL AND z = 5

If z is computed before y IS NOT NULL the query will be in trouble.

> Is there any workaround other than doing something horrible like below
>
> select *
> from (select t.a + t.b + t.c Total
> from some_table t)
> where Total > 0
That's how how set processing works.
[Quoted] You will need to _accept_ it if you want to survive with SQL. You will need to learn to _appreciate_ it if you want to become good with SQL. :-)

Cheers
Serge

-- 
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Received on Thu Mar 10 2005 - 00:28:53 CET

Original text of this message