RoiEX RoiEX - 1 year ago 109
SQL Question

Calculating with aliases

Simple question:

Can I use an alias to calculate with? Is

SELECT example1 / example2 AS ratio
, SQRT(ratio) AS squareroot
FROM example_table

legal and are there special rules to it (defining alias before using it etc.)?

Answer Source

No, you should define an alias before using it, so you either:

select example1 / example2 as ratio
     , sqrt(example1 / example2) as squareroot 
from example_table


select ratio
     , sqrt(ratio) as squareroot
from ( 
  select example1 / example2 as ratio 
  from example_table
) as tbl
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download