SUM

<< Click to Display Table of Contents >>

Navigation:  Apollo SQL > Aggregate Functions >

SUM

Returns the sum of all values in a column. To find out how many singles have been hit in a baseball game, use:

SELECT SUM(singles) Total_Singles FROM teamstats

Result:

Total_Singles

-------------

174

 

To get several sums, use:

SELECT SUM(singles) Total_Singles, SUM(doubles) Total_Doubles,

SUM(triples) Total_Triples, SUM(hr) Total_HR FROM teamstats

Result:

Total_Singles Total_Doubles Total_Triples Total_HR

------------- ------------- ------------- --------

174 32 5 29

To collect similar information on all players with a batting average of .300 or better, use:

SELECT SUM(singles) Total_Singles, SUM(DOUBLES) Total_Doubles,

SUM(triples) Total_Triples, SUM(hr) Total_HR FROM teamstats  

FROM teamstats WHERE HITS/AB >= .300

Result:

Total_Singles Total_Doubles Total_Triples Total_HR

------------- ------------- ------------- --------

164 30 5 29

To compute a team batting average, use:

SELECT SUM(hits)/SUM(ab) Team_Average FROM teamstats

Result:

Team_Average

------------

.33706294

SUM works only with numeric values, and will not work on non-numeric fields. The following are additional valid examples of using SUM:

SELECT SUM(Score * Questions) FROM Scores

 

SELECT SUM(Score * Questions) As SumScores FROM Scores

 

SELECT SUM(DISTINCT Score * Questions) As SumScores FROM Scores

 

SELECT SUM(Score * Questions) / SUM(Questions) FROM Scores

 

SELECT FORMAT('%.2f', SUM(Score * Questions)) FROM Scores