• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 46
  • 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 SontakkeDeveloperCommented:
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

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

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