I have a sql query that I am trying to aggregate counts into, and I'm running into an issue. I've got a table that connects a user to a couple of categories they have saved. UsersToCategories. So the three tables combined would be Users, Categories and UsersToCategories. The following query will give me a list of all categories belonging to a user:
select c.CategoryID, c.CategoryName, utc.SortOrder
from Categories c
inner join UsersToCategories utc on utc.CategoryID = c.CategoryID
where utc.UserID = 1234
I have a further level of detail which has Services. There can be multiple services within a category, in a one-to-many relationship. So you've got Categories from above, Services, and ServicesToCategories
What I need is a way to join that second piece of pseudocode into the top query I wrote so that I get COUNTS of how many services in each category. At this level I don't need to know what the services are, just a raw count of each.
I know this is sort of basic stuff, but I'm not that great at sql, and I can't seem to find an exact answer to my question. Any help is appreciated.