使用 select 语句的计算结果计算单个语句中的另一列

Using calculated results of select statement to calculate another column in single statement

我有一个这样的 SQL 语句:

Insert into @Matches(RowId, PercentMatch) select distinct t.[Row],
cast(Max(Counts) as float)/(Abs(Max(Counts) - case when Max(VarLength) >= @tempint then Max(VarLength) else @tempint end) + Max(Counts))* 100 
As MatchingPercent from @Temp t Group by [Row] order by MatchingPercent desc

我的下一个 sql 语句是:

Update @Matches set Percentage =  PercentMatch * @constantVal

我想强调一下 PercentMatch 列在上面的 Insert into select 语句中是 "calculated"。

我想做什么:我如何将它合并到一个语句中?

为什么我需要这个:

它位于由 20-30 个异步执行的存储过程(使用服务代理)执行的过程内部。

您可以将 SELECT 放在子查询中,并通过在外部查询中执行乘法,在 INSERT 语句中插入 Percentage 值:

Insert into @Matches(RowId, PercentMatch, Percentage) 
select tRow, MatchingPercent, MatchingPercent * @constantVal
from (
   select distinct t.[Row] as tRow, 
         cast(Max(Counts) as float) / (Abs(Max(Counts) 
         - 
         case 
           when Max(VarLength) >= @tempint then Max(VarLength) 
           else @tempint 
         end) + Max(Counts))* 100 As MatchingPercent 
from @Temp t 
Group by [Row] ) s

我也认为 order by 子句在 INSERT INTO SELECT 语句中没有多大用处。