Page 1 of 1

[Solved] Using Alias filed within a query

Posted: Fri Oct 05, 2018 1:47 pm
by Maax555
Hi. is it not possible to use the result of an alias in the same query? I have an calculation using alias GM for example in a query. In the same query I then want to use the GM to create another alias?

Re: Using Alias filed within a query

Posted: Fri Oct 05, 2018 5:16 pm
by eremmel
I do not follow you. Normally you can use table-aliasses at any place you can use a table-name; field(expression)-aliases can only be used in order by or in query that contains a derived table in which an field(expression)-alias is used.
Please post the your query for better understanding.

Re: Using Alias filed within a query

Posted: Tue Oct 09, 2018 9:07 am
by Maax555
Hi thanks, i found a different way to achieve my goal now. However i was trying to use the the resulting alias field, lets say GM in the same query. So i had two fields from queries to generate the alias GM then i wanted to use the GM result in same query to make a new alias, lets say GM x 1.33 = new alias GMResult. I was getting an error saying GM did not exist.

Re: Using Alias filed within a query

Posted: Tue Oct 09, 2018 7:51 pm
by UnklDonald418
HSQL parses a SELECT statement all at once so an alias can't be used for a calculation in the same statement where it was defined.

Code: Select all

SELECT X AS GM, GM * 1.33 AS "GMResult" FROM "SomeTable" 

generates the Column not found exception.
You would need to use the original field name for the calculation in the SELECT statement. You can use the newly defined alias in a WHERE statement because it is parsed after the SELECT statement.

Code: Select all

SELECT X AS GM, X * 1.33 AS "GMResult" FROM "SomeTable"  WHERE GM > 1
I can't imagine why you would require using an alias but I suppose you could use something silly, like

Code: Select all

SELECT GM, GM * 1.33 AS "GMResult" FROM "SomeTable", (SELECT X AS GM FROM "SomeTable")

Re: Using Alias filed within a query

Posted: Tue Oct 23, 2018 10:38 am
by Maax555
many thanks for help on this one. I ended up using another query and an alias.