Select Sum (Amount) As Total: Customers - Customerid Sales - Customerid
Select Sum (Amount) As Total: Customers - Customerid Sales - Customerid
Application
Data aggregation is the process of taking numerous records and collapsing them into a single
summary record. When aggregating data, one must decide what records must be considered in
the summary and how these records should be summarized. Data can be summarized based on
certain fields (attributes) in a database or derived attributes.
The examples below were performed in SQL Server 7.0 so the syntax for these may be slightly
different if you are working with a different DBMS.
In the above example with a date comparison using a string representation of a date. This
represents the dataset that we will summarize.
*Note SQL Server 7 does an implicit string to date conversion. Some DBMS require
explicit casting such as CAST to date or CONVERT. Also valid string representations of
dates may vary from local or DBMS system.
Once the data to summarize has been decided on, the next step is to decide how to
aggregate the data. Data can be aggregated in an infinite number of ways. For example if
we are doing an analysis of what are the best days of the week for sales we may want to
know the average sale per day of week.
SELECT DATEPART(dw,SaleDate) As DayOfWeek, AVG(Amount) AvgDaySale
FROM Sales
WHERE SaleDate > '1999-12-30'
GROUP BY DATEPART(dw,SaleDate)