别名postgresql查询中的长重复表达式

时间:2014-01-30 14:10:48

标签: postgresql

当select和where块中使用相同的表达式时,postgresql中有没有办法不重复自己?

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 个答案:

答案 0 :(得分:3)

你可以这样做:

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