how to use aiias name in where clause


I tried to use alias name for the field in the where clause, and it will not work, anyone knows how to work around it?







etai's picture
Joined: Jun 18 2014 - 2:21pm
Last seen: 9 years 1 month ago

Wrap your query that uses alias as a sub query and use main query to reference the alias. For example:

select * from (
select t.tenantname as organization
, u.username as user
, r.rolename as role
from jiuser u
inner join jiuserrole j on j.userid =
inner join jirole r on = j.roleid
inner join jitenant t on = u.tenantid
) t
where organization = 'root'


tchen - 9 years 1 month ago

tchen is correct:  the column aliases are established in the SELECT clause, but the WHERE clause is run after the FROM (and can therefore use table aliases) but before the SELECT.  So you either user the actual value like t.tenantname in our WHERE (or even repeat a calculation or function if necessary), or else if the manipulation you did in the SELECT (calculations/functions/CASE statements, etc.) was too taxing or wordy to repeat in your WHERE clause, then you can instead wrap an outer query around the inner one, and the entire outer query (including a WHERE clause) has all of the column aliases from the inner query in scope, e.g.:

    when tran.posting_date is null then 'Null'
    when tran.posting_date<CURRENT DATE - 1 year then 'Long ago'
    when tran.posting_date<CURRENT DATE then 'Recent past'
    when tran.posting_date=CURRENT DATE THEN 'Today'
    else 'Future'
  end as date_category,
  tran.amount as amount
  date_category in ('Long ago', 'Future')




cbarlow3 - 9 years 1 month ago

it works fine,  thanks for the tips

etai - 9 years 1 month ago

0 Answers:

No answers yet