Alias the long repeated expression in postgresql query

Is there any way in postgresql to not repeat myself when the same expression is used in select and where blocks?

select (
    |/(( power((consumption_2011 - consumption_3y/3), 2)
       + power((consumption_2012 - consumption_3y/3), 2)
       + power((consumption_2013 - consumption_3y/3), 2)
    ) / 3)) as deviation
from consumption
where (
    |/(( power((consumption_2011 - consumption_3y/3), 2)
       + power((consumption_2012 - consumption_3y/3), 2)
       + power((consumption_2013 - consumption_3y/3), 2)
    ) / 3) > 0.8
)

Answers


You can do something like that:

select deviation 
from (
select 
    (( power((consumption_2011 - consumption_3y/3), 2)
      + power((consumption_2012 - consumption_3y/3), 2)
      + power((consumption_2013 - consumption_3y/3), 2)
    ) / 3) as deviation
from consumption) sub
where deviation > 0.8

Need Your Help

Error while trying to retrieve expired object from memcached

mysql memcached innodb

I'm trying to learn how to use the memcached plugin in MySQL 5.6. I used telnet to get and set values to memcached. When I try to get an expired value from memcache, it says "Connection closed by f...

c++ openmp shared_ptr datarace

c++ openmp

I have a c++ program where I try to use openmp on a for loop. The for loop works with an shared_ptr to my own class that in turn calls another dll. I get errors: