Count Number of Items in each Category

Count Number of Items in each Category

am 10.04.2006 05:47:25 von B-Dog

I'm trying to list my categories and how many items are in each
category. I'm not much on sub queries. How can I combine these two
items. I'd like to display the category and the corresponding number of

items in each

Cats:
Select
Category.CategoryID,
Category.Category,
Category.Sorting
From
Category
Order By
Category.Sorting Desc


Items:
Select Count(Listings.CategoryID)
From
Listings
Group By
Listings.CategoryID


I've tried the following but can't get it to work:


Select
Category.CategoryID,
Category.Category,
Category.Sorting,
(Select Count(Listings.CategoryID)
From
Listings WHERE Listings.CategoryID = Category.CategoryID) AS cnt
>From
Category
Order By
Category.Sorting Desc

Re: Count Number of Items in each Category

am 10.04.2006 18:15:43 von onedbguru

Select
Category.CategoryID,
Category.Category,
Category.Sorting,
Count(Listings.CategoryID) as cnt
>From Category, Listing
WHERE Listings.CategoryID = Category.CategoryID)
group by
Category.CategoryID,
Category.Category,
Category.Sorting
Order By
Category.Sorting Desc

If you are going to continue doing SQL - a good reference book would be
in order. Also - the term (in this case) that you would use for Google
would be Joins, Cross Joins