Reputation: 60014
hive
rejects this code:
select a, b, a+b as c
from t
where c > 0
saying Invalid table alias or column reference 'c'
.
do I really need to write something like
select * from
(select a, b, a+b as c
from t)
where c > 0
EDIT:
c
it complex enough for me not to want to repeat it in where a + b > 0
hive
Upvotes: 4
Views: 8862
Reputation: 65
You can run this query like this or with a Common Table Expression
select a, b, a+b as c
from t
where a+b > 0
Reference the below order of operations for logical query processing to know if you can use derived columns in another clause.
Keyed-In Order
Logical Querying Processing Phases
Upvotes: 3
Reputation: 1870
no
just:
select a, b, a+b as c from t where a+b > 0
note: for mysql at least: order by and group by you can use column (or expression) positions
e.g. group by 2, order by 1 would get you one row per column 2 (whether a field name or an expression) and order it by column 1 (field or expression)
also: some RDBMS's do let you refer to the column alias as you first attempted
Upvotes: 0
Reputation: 883
It would have to look like this:
select a, b, a+b as c
from t
where a+b > 0
An easy way to explain/remember this is this: SQL cannot reference aliases assigned within its own instance. It would, however, work if you did this:
SELECT a,b,c
FROM(
select a, b, a+b as c
from t) as [calc]
WHERE c > 0
This syntax would work because the alias is assigned in a subquery.
Upvotes: 1
Reputation: 5689
Use a Common Table Expression if you want to use derived columns.
with x as
(
select a, b, a+b as c
from t
)
select * from x where c >0
Upvotes: 5
Reputation: 3137
You are close, you can do this
select a, b, a+b as c
from t
where a+b > 0
Upvotes: 1