Database Reference
In-Depth Information
Try It: Using the MIN, MAX, SUM, and AVG Functions
Let's find the minimum, maximum, sum, and average of the unit price (UnitPrice) of each sales order
(SalesOrderID) from the SalesOrderDetail table.
Open a New Query window in SQL Server Management Studio. Enter the following query, and click
Execute. You should see the results shown in Figure 5-12.
select SalesOrderID,min(UnitPrice)as "Min",
max(UnitPrice) as "Max",Sum(UnitPrice) as "Sum",
Avg(UnitPrice)as "Avg"
from Sales.SalesOrderDetail
where SalesOrderID between 43659 and 43663
group by SalesOrderID
Figure 5-12.
Using aggregate functions
How It Works
You use the
MIN
and
MAX
functions to find the minimum and maximum values, the
SUM
function to
calculate the total value, and the
AVG
function to calculate the average value.
min(UnitPrice) as "Min",
max(UnitPrice) as "Max",
Sum(UnitPrice) as "Sum",
Avg(UnitPrice)as "Avg"