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 2000 Forums
 Transact-SQL (2000)
 CategoryName (0)

Author  Topic 

chexone
Starting Member

6 Posts

Posted - 2006-10-04 : 14:25:16
Hi!

The following query returns a category name and a count of items in the category:
Cat1 4
Cat2 5
Cat3 2

But it does not return categories where the count is 0. How would I change this query to have it return 0 count cats too? Thanks!

SELECT tblServicesCats.catID, tblServicesCats.catName, COUNT(tblServices.serviceID) AS Total
FROM tblServicesCats INNER JOIN tblServices ON tblServicesCats.catID = tblServices.serviceCat
GROUP BY tblServicesCats.catID, tblServicesCats.catName, tblServices.serviceCat
ORDER BY tblServicesCats.catName Asc

Kristen
Test

22859 Posts

Posted - 2006-10-04 : 14:52:32
Changing the INNER JOIN to a LEFT OUTER JOIN should do it

Kristen
Go to Top of Page

chexone
Starting Member

6 Posts

Posted - 2006-10-04 : 15:10:00
Nice! That did it. Thanks!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-10-05 : 02:08:12
You might be getting a grizzly warning about NULLs included in the aggregate, and that can sometimes upset applications. There are ways around that if it is a problem, and if it isn't a problem I'll just sit down and shut up!

Kristen
Go to Top of Page
   

- Advertisement -