首页 > 解决方案 > 如何将列乘以从先前查询中获得的值?

问题描述

我计算了表中两列的斜率和截距:

select regr_slope(x, y) slope, regr_intercept(x, y) intercept
from table

现在我想将列 x 与斜率相乘并添加截距,如下所示:

select x, y, slope * x + intercept as y_fit 
from table

如何在一个查询中实现这一目标?

标签: sqlpostgresql

解决方案


您可以使用窗口函数:

select x, y,
      regr_slope(x, y) over () * x + regr_intercept(x, y) over () as y_fit 
from table

推荐阅读