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.
| Author |
Topic |
|
sbayeta
Starting Member
3 Posts |
Posted - 2010-06-08 : 07:58:23
|
| Hi,I'm trying to count the records on one table based on an id in a different table. Something like this:Table customerid customer--------------1 John2 Mike3 CarlTable orderid custid amount----------------------1 2 1002 2 403 1 754 1 355 1 1006 1 40I'd like to create a query that will return the orders count for each customer, but without omiting the customers that don't have orders:custid orders----------------1 42 23 0How can I do this?Thanks in advance.Best regards,Santiago |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2010-06-08 : 08:11:12
|
| Select c.id,count(o.id) as orders from customer c left join order o on o.custid =c.id group by c.idSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
sbayeta
Starting Member
3 Posts |
Posted - 2010-06-08 : 09:00:06
|
| I omited a small detail. I need a where clause to limit to orders above 50. If I add this clause the query returns no records for customers with no orders matching the criteria.Any ideas?Thanks |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-06-08 : 09:03:59
|
| having count(o.id) >50 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-06-08 : 09:08:48
|
In that case, use INNER JOIN instead of LEFT JOIN, if you always is having the "number of orders" criteria. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|
|