Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 count of orders for each customer

Author  Topic 

getur.srikanth@gmail.com
Yak Posting Veteran

77 Posts

Posted - 2008-03-03 : 16:40:48
Hi,

I have two tables customers and orders. customerID is the foreign key for order table. If I pass customername I need to get information about each customer how many orders holding?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-03 : 16:45:32
But if you pass a customername, you will get orders for that customer only.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-03 : 16:46:27
SELECT c.CustomerName, COUNT(o.CustomerID)
FROM Customers AS c
LEFT JOIN Orders AS o ON o.CustomerID = c.ID
GROUP BY c.CustomerName
ORDER BY c.CustomerName



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

getur.srikanth@gmail.com
Yak Posting Veteran

77 Posts

Posted - 2008-03-03 : 17:15:42
Thanks, I got it

quote:
Originally posted by Peso

SELECT c.CustomerName, COUNT(o.CustomerID)
FROM Customers AS c
LEFT JOIN Orders AS o ON o.CustomerID = c.ID
GROUP BY c.CustomerName
ORDER BY c.CustomerName



E 12°55'05.25"
N 56°04'39.16"


Go to Top of Page
   

- Advertisement -