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

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

using COUNT in t-sql

i'm using sql server 2008

FROM [Northwind].[dbo].[Orders]
ORDER BY [CustomerID]

This is the Orders table

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.

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.

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