1

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
)

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.