我有一个这样的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
列是"计算"在上面插入到select语句中。
我想做什么:我如何将其合并到一个声明中?
为什么我需要这个:
它位于由asynch(使用Service broker)执行的20-30个存储过程执行的过程内部。
答案 0 :(得分:1)
您可以将SELECT
置于子查询中,并通过在外部查询中执行乘法在Percentage
语句中插入INSERT
值:
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
语句中没有多大用处。