我有一个SQL变量@SumScore dec(9,4)
我正在尝试将变量分配如下:
SET @SumScore =
(
SELECT Sum(
(
SELECT SUM(etjs.CalculatedScore * sc.PercentOfTotal) as CategoryScore
FROM tblEventTurnJudgeScores etjs
INNER JOIN tblJudgingCriteria jc ON jc.JudgingCriteriaID = etjs.JudgingCriteriaID
INNER JOIN tblScoringCategories sc ON jc.ScoringCategoryID = sc.ScoringCategoryID
GROUP BY jc.JudgingCriteriaID
)
As ComputedScore) AS SumTotalScore
)
换句话说,内部选择返回一列。我希望为var分配返回到那里的所有行的总和。
我意识到可以使用临时表轻松完成此操作。但这是唯一的方法吗?
最佳答案
用:
SET @SumScore = SELECT SUM(etjs.CalculatedScore * sc.PercentOfTotal) as CategoryScore
FROM tblEventTurnJudgeScores etjs
JOIN tblJudgingCriteria jc ON jc.JudgingCriteriaID = etjs.JudgingCriteriaID
JOIN tblScoringCategories sc ON jc.ScoringCategoryID = sc.ScoringCategoryID
如果外部查询仍然要汇总所有内容,则没有必要使用
GROUP BY jc.JudgingCriteriaID
。关于sql-server - 如何汇总返回多行的选择结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1950256/