如何在涉及更新语句的查询中加入多个 select 语句的结果

How to join results of multiple select statements ina query that involves update statements

我有一个 table,其中包含 Y、X1、X2 和 W 列。我正在对每一列进行操作

declare @std float
set @std = (select SUM(W*Col) from table)
update table set Col=sqrt(W)*(Col-@std)
select @std

我如何有效地将这些查询组合到每一列和 return 3 个值 [@std for each column]

这符合您的定义,尽管根据列自身的值更新列有点奇怪。我希望您只打算 运行 此查询一次。

DECLARE
    @std_y  FLOAT,
    @std_x1 FLOAT,
    @std_x2 FLOAT

SELECT
    @std_y =  SUM(W*Y),
    @std_x1 = SUM(W*X1),
    @std_x2 = SUM(W*X2)
FROM
    dbo.My_Table

UPDATE dbo.My_Table
SET
    Y  = SQRT(W) * (Y  - @std_y),
    X1 = SQRT(W) * (X1 - @std_y),
    X2 = SQRT(W) * (X2 - @std_y)

SELECT
    @std_y  AS std_y,
    @std_x1 AS std_x1,
    @std_x2 AS std_x2