I have a SQL variable @SumScore dec(9,4)
I am trying to assign the variable as follows:
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
)
In other words the inner select is returning one column. I want the var to be assigned the SUM of all of the rows that are being return there.
I realize that this could be done with a temp table pretty easily. But is that the only way?
If you need to sum a column or row of numbers, let Excel do the math for you. Select a cell next to the numbers you want to sum, click AutoSum on the Home tab, press Enter, and you're done. When you click AutoSum, Excel automatically enters a formula (that uses the SUM function) to sum the numbers.
If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; The SELECT statement in SQL tells the computer to get data from the table.
To do this: Select the data to sum plus the blank row below the data and the blank column to the right of the data where the totals will display. On the “Home” tab, in the “Editing” group, click the AutoSum button. Totals are calculated and appear in the last row and in the last column of the selected range!
SELECT Sum(CategoryScore)
FROM ( subquery )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With