Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with aggregate functions

Status
Not open for further replies.

againstTheWind

Programmer
Jun 22, 2006
42
US
I am trying to determine the number of repeat customers we have.

The data is an ORDERS table where using:

select count(CustomerID) as OrdersMade
from Orders
group by customerID

will provide the total number of orders for each customer. Example:

CustomerID OrdersMade
5456 2
4547 1
5667 5

However I need to know how many customers made 3, 4, 5, 6, etc... orders. Thus the date would look like this:

OrdersMade NumberOfCustomers
1 99999999
2 56789
3 23000
4 1200

etc....

Any ideas?

Thanks a bunch!
 
There may be a better way, but I would select your first set of results into a temp table. Then use that temp table to get your final count.

Lets say you select into #temp:
Code:
Select OrdersMade, Count(OrdersMade) As NumberOfCustomers
From #Temp
Group By OrdersMade
Order By OrdersMade

Jim
 
Code:
SELECT A.TotalOrders AS NumberOfOrders, COUNT(A.CustomerID) AS NumberOfCustomers
FROM (SELECT CustomerID, COUNT(CustomerID) AS TotalOrders
		FROM Orders
		GROUP BY CustomerID) A
GROUP BY A.TotalOrders

The inner query will get the number of orders per each customer in the database

Jack – 13 Orders
Joe - 1 Order
Sue – I Order

Etc

The outer query will count the customers grouping them by their order number, this will give you what you want. Hope this will help


Walid Magd (MCP)

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
Sorry Denis
We posted at the same time I think.

Walid Magd (MCP)

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top