Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sum the results of a select that returns multiple rows

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?

like image 449
Seth Spearman Avatar asked Dec 23 '09 01:12

Seth Spearman


People also ask

How do I sum values from different rows?

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.

How do I sum query results in SQL?

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.

How do I sum cells from different rows in Excel?

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!


1 Answers

SELECT Sum(CategoryScore)
FROM ( subquery )
like image 93
Tor Valamo Avatar answered Oct 17 '22 19:10

Tor Valamo