Summarizing Data with Aggregate Function

A query can produce generalization about groups of values as well as individual column values. It does this through the use of aggregate function. Aggregate function produce a single value for entire group of columns. Here is a list of these function:

  • COUNT – Produce the number of rows or non –NULL column values that the query select
  • SUM – Produce the arithmetic sum of all selected values of given column.
  • AVG – Produce the average (mean) of all selected value of a given column.
  • MAX – Produce the largest of all selected values of given column.
  • MIN – Produce the smallest of all selected of given column.

How Do You Use Aggregate Function?

Aggregate functions are used like column names in the SELECT clause of queries and, with one exception, take column name as argument. Only numeric column can be used for SUM and AVG. for COUNT, MAX and MIN, any datatype can be used. When used with character column, MAX and MIN will be interpreted in term of collection which is to say that, generally speaking, MIN will means first and MAX last , in alphabetical order. NULL value are ignore in all calculations.

To find Sum of all of all of our purchases from order table, we could enter the following query, those output is show in figure.

  1. SELECTSUM(AMT)FROM #tempOrder  
Aggregate Function

Special Attribute of COUNT

The COUNT function is slightly different. It counts the number of values in a given column or number of rows in a table. When it is counting column values, you can combine it with DISTINCT to produce account of the different values in a given column.

Counting Values – we could use count, for example, to count the visitor currently listed in #VISITOR table show in figure.
  1. CREATETABLE #VISITOR(ID INTIDENTITY(1,1),VISITORID INT,DATEOFVISIT DATETIME)  
  2.   
  3. INSERTINTO #VISITOR  
  4.   
  5. SELECT 1,GETDATE()UNIONALLSELECT 2,GETDATE()UNIONALLSELECT 3,GETDATE()UNIONALLSELECT 2,GETDATE()UNIONALLSELECT 1,GETDATE()  
  6. UNIONALLSELECT 4,GETDATE()UNIONALLSELECT 5,GETDATE()UNIONALLSELECT 2,GETDATE()UNIONALLSELECT 2,GETDATE()  
  7.   
  8.   
  9. SELECTCOUNT(VISITORID) [WITHOUT DISTINCTFROM #VISITOR  
  10.   
  11. SELECTCOUNT(DISTINCT VISITORID ) [WITH DISTINCTFROM #VISITOR  
COUNT

If we had not specified DISTINCT, we would have gotten a count of all rows that had duplicate or non-NULL values.

AVG: AVG function is used to produce the average of selected value
  1. SELECTAVG(AMOUNT) AVGAMOUNT FROM dbo.CustomerOrders  
AVG

MAX: This function produce the largest of all selected values of given column

  1. SELECTMAX(AMOUNT) AVGAMOUNT FROM dbo.CustomerOrders  
MAX

Don’t Nest Aggregates

In a strict International ISO SQL, you cannot take an aggregate of an aggregate. Consider the following query.
  1. SELECT VISITORID,max(sum(VISITORID)) [WITHOUT DISTINCTFROM #VISITOR groupby VISITORID  
Nest Aggregates

Your statement will probably be rejected.

 

Next Recommended Reading Aggregate Function in SQL Server