How do I get rid of the duplicate code in this query? I have this query, which works:
SELECT *, (&quot;last done&quot; + &quot;auto time span&quot;::interval) AS &quot;new last done&quot; 
FROM personal.&quot;time tabs&quot; 
WHERE &quot;reset automatically&quot; = true 
 AND (&quot;last done&quot; + &quot;auto time span&quot;::interval) &lt; now();

However, I notice the duplicate code: (&quot;last done&quot; + &quot;auto time span&quot;::interval). Is it possible to make it only mentioned once?
These did sadly NOT work:
SELECT *, (&quot;last done&quot; + &quot;auto time span&quot;::interval) AS &quot;new last done&quot; 
FROM personal.&quot;time tabs&quot; 
WHERE &quot;reset automatically&quot; = true 
AND &quot;new last done&quot; &lt; now();

Or:
SELECT * 
FROM personal.&quot;time tabs&quot; 
HAVING (&quot;last done&quot; + &quot;auto time span&quot;::interval) AS &quot;new last done&quot; 
WHERE &quot;reset automatically&quot; = true 
 AND &quot;new last done&quot; &lt; now();

What is the right way?
I would just duplicate the expression. It is possible to structure your query so that you only define the expression once, but it will end up being more verbose, as it requires that you define the field you're WHEREing as a column in a subquery and then do the WHERE on the outer field.
SELECT * FROM (
  SELECT *, (&quot;last done&quot; + &quot;auto time span&quot;::interval) AS &quot;new last done&quot;
  FROM personal.&quot;time tabs&quot;
  WHERE &quot;reset automatically&quot; = true
) AS inner_query
WHERE &quot;new last done&quot; &lt; now();

As long as you don't have ORDER BY or LIMIT on the inner query, the query planner should be able to tell that it's logically equivalent to having that WHERE in the inner query, so actual query execution shouldn't be affected.