我知道这个问题可能已得到解答,但我遇到的很多例子都涉及使用INNER JOIN将数据从一个表传输到另一个表,我的情况有点不同。
基本上我正在尝试将此SELECT语句转换为Update语句。
SELECT
SR, 100. * Cast(Round(AVG(Three_1 + Three_2 + Three_3 + Three_4 + Three_5 + Three_6 + Three_7 + Five_1 + Six_1 + Six_3) / 38,0) as int)
FROM
MainDB
WHERE
Ctype = 'Call'
GROUP BY
SR
我能够成功计算一些列的百分比,然后我想用这些信息更新列。我的第一次尝试就是这个
UPDATE QAMainDB
SET TCX_score = 100. * avg(three_1 + Three_2 + Three_3 + Three_4 + Three_5 + Three_6 + Three_7 + Five_1 + Six_1 + Six_3) / 38
我收到了错误:
聚合可能不会出现在UPDATE语句的集合列表中。
在做了更多的跟踪和错误后,我尝试了这种方法来声明Stack上的前一个例子的变量。
Declare @Result Decimal = 0
SET @Result = 100. * Cast(round(avg(three_1 + Three_2 + Three_3 + Three_4 + Three_5 + Three_6 + Three_7 + Five_1 + Six_1 + Six_3) / 38,0) as int)
UPDATE QAMainDB
SET TCX_score = @Result
WHERE Ctype = 'Call'
但它失败了,我想知道你们中是否有人可以通过引导我朝着正确的方向帮助我。我对SQL很陌生,所以请耐心等待。
感谢您的时间。
答案 0 :(得分:3)
你如何匹配列?我猜测SR
是用于表之间匹配的列:
WITH m as (
SELECT SR, 100. * Cast(Round(AVG(Three_1 + Three_2 + Three_3 + Three_4 + Three_5 + Three_6 + Three_7 + Five_1 + Six_1 + Six_3) / 38,0) as int) as val
FROM MainDB
WHERE Ctype = 'Call'
GROUP BY SR
)
Update QAMainDB
SET TCX_score = val
FROM QAMainDB JOIN
m
ON m.SR = QAMainDB.SR;
答案 1 :(得分:2)
尝试将您的代码更改为此代码。
Update QAMainDB SET TCX_score = (select 100. * avg(three_1 + Three_2 + Three_3 + Three_4 + Three_5 + Three_6 + Three_7 + Five_1 + Six_1 + Six_3) / 38)