我有这样的选择查询:
select
SUM(Percentage) as SUM,
@cnt as Count,
(SUM(Percentage) / @cnt) as Percentage
from
#Temp2
group By
RowNumber
order by
Percentage desc
Run Code Online (Sandbox Code Playgroud)
Percentage 上面的查询列看起来像这样:

然后我在上面的查询下面有一个查询:
Update dbo.ResultsStored
set FinalSearchSeral = @searchNumber,
ModifiedAt = getDate(),
PercentMatch = t.Perc
from
(select (SUM(Percentage) / @cnt) as Perc
from #Temp2
GROUP BY RowNumber) t
where
HashedKey = HASHBYTES('MD5', @StringConcat)
select *
from dbo.ResultsStored
order by PercentMatch desc
Run Code Online (Sandbox Code Playgroud)
注意:where这里的子句不是问题,因为我打算仅使用匹配的哈希码代码更新行.
select语句的结果令我感到困惑.
上面的select语句导致Percentage列包含以下结果:

我不明白为什么用上述查询计算的百分比不同?
但是,第一个查询结果正确,第二个查询出错.
结果是预期的,在更新中你需要指定哪个字段应该从我的意思使用的查询中获取哪个值Join,试试这个(正如你在评论中提到的,如果rowid可以是一个连接列):
Update dbo.ResultsStored
set dbo.ResultsStored.FinalSearchSeral = @searchNumber,
dbo.ResultsStored.ModifiedAt = getDate(),
dbo.ResultsStored.PercentMatch = t.Perc
from
dbo.ResultsStored
join
(select RowId,(SUM(Percentage) / @cnt) as Perc
from #Temp2
GROUP BY RowId) t
on dbo.ResultsStored.RowId=t.RowId
and dbo.ResultsStored.HashedKey = HASHBYTES('MD5', @StringConcat)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |