Boy, do I LOVE GROUP BY!
Assume a table of invoice values
InvoiceID
InvoiceDate
CustomerID
SalesmanID
Amount
Suppose you wanted to see a summary of all invoice amounts by date
SELECT InvoiceDate, sum(amount)
FROM invoice
GROUP BY InvoiceDate
Suppose you wanted to see a summary of all invoice amounts by Customer
SELECT CustomerID, sum(amount)
FROM invoice
GROUP BY CustomerID
Ssummary of all invoice amounts by Salesman
SELECT SalesmanID, sum(amount)
FROM invoice
GROUP BY SalesmanID
You can get more interesting by using JOINS
Assume each salesman is in a region table
SalesmanID
RegionID
This would summarize regions:
SELECT RegionID, sum(amount)
FROM invoice
inner join region on invoice.SalesmanID=region.SalesamanID
Group by RegionID
Assuming a customer table
CustomerID
Zipcode
This would summarize by zipcode:
SELECT Zipcode, sum(amount)
FROM invoice
inner join customer on invoice.customerID=customer.ID
Group by Zipcode
Here's a fun one: same code, slightly modified summarizes by metro area:
SELECT Substring(Zipcode,1,3), sum(amount)
FROM invoice
inner join customer on invoice.customerID=customer.ID
Group by Substring(Zipcode,1,3)
etc