COMPUTE
COMPUTE is an SQL command used to generate report-like results with subtotals and totals in the query result set. It groups the result set and applies aggregate functions, providing calculations of numeric column values within the set.
Example
Output
Explanation
The SELECT query retrieves and sorts data from the Sales.SalesPerson
table where SalesYTD
is not equal to 0. The results are listed according to TerritoryID
.
Next, the COMPUTE
clause calculates the sum and average of SalesYTD
for each TerritoryID
. The result set accordingly displays these computed values after each set of TerritoryID
data.