I want to reuse an annotation’s alias in subsequent expressions. How can I make Django generate SQL like the following:
select
a*b as x
x/d as y
from my_table;
Note how x
is simply referenced in x/d
, rather than being expanded into (a*b)/d
. Is this possible in Django?
The reason I want to do this is because my queries are very large and complex. When using regular annotations, Django is duplicating large expressions many times and the resulting query output is becoming overly complex and difficult to debug and ensure correctness.
4