首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用select语句的计算结果计算单个语句中的另一列

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

Stack Overflow用户
提问于 2015-03-30 13:54:45
回答 1查看 26关注 0票数 0

我有这样一个SQL语句:

代码语言:javascript
复制
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语句是:

代码语言:javascript
复制
Update @Matches set Percentage =  PercentMatch * @constantVal

我想强调指出,PercentMatch列在上面的Insert Insert select语句中是“计算”的。

我想做什么:如何在一条语句中合并它?

为什么我需要这个:

它位于由异步执行的20-30个存储过程(使用Service )执行的过程中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-30 14:02:29

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

代码语言:javascript
复制
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语句中没有多大用处。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29348057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档