etai Posted July 29, 2014 Posted July 29, 2014 Hello: 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? thanks Eric
Tom C Posted July 29, 2014 Posted July 29, 2014 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 rolefrom jiuser uinner join jiuserrole j on j.userid = u.idinner join jirole r on r.id = j.roleidinner join jitenant t on t.id = u.tenantid) twhere organization = 'root'
cbarlow3 Posted July 29, 2014 Posted July 29, 2014 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.:select date_category, amountfrom(select case 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 amountfrom tran)where date_category in ('Long ago', 'Future')
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now