Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 32
  • Last Modified:

using COUNT in t-sql

i'm using sql server 2008

SELECT [OrderID]
      ,[CustomerID]
      ,[EmployeeID]
FROM [Northwind].[dbo].[Orders]
ORDER BY [CustomerID]

This is the Orders table

p1.PNG
I want to use the COUNT(*) function to get the count of Orders for each CustomerID.

How do I get the Count of orders for each CustomerID.

The result returned from the query should look lik this.
The first column would be the CustomerID and then the second column would be the count of Orders that customer had.
So then for example in the image below. ALFKI had 6 orders, ANATR 4 orders and so forth.

p2.PNG
0
maqskywalker
Asked:
maqskywalker
1 Solution
 
Nitin SontakkeCommented:
Try following:

SELECT [CustomerID]
      ,COUNT(1) [Orders]
FROM [Northwind].[dbo].[Orders]
group by [CustomerID]
ORDER BY [CustomerID]

Open in new window


Not sure what is the significance of EmployeeId in your query.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now