Use computed columns more than once in same query

Viewed 64

I have this recurring issue where I want to use a value I computed in a select query more than once. Here's an example

SELECT
  complicated_function(x) as foo,
  another_complicated_function(y) as bar,
  complicated_function(x)/another_complicated_function(y) as foo_bar_rate
FROM my_table;

What's the simplest way to write this query? Ideally, I'd like to write

SELECT
  complicated_function(x) as foo
  another_complicated_function(y) as bar,
  foo/bar as foo_bar_rate
FROM my_table;

This question isn't about the specific values being computing, it's about how to write this query in a simpler way that can be more easily maintained.

2 Answers
Related