I was wondering if someone could help me with chaining joins - I'm not understanding the thought process.
An example with three tables:
ArticleCategories
-----------------
CategoryID
CategoryName
Articles
---------
ArticleID
ArticleText
CategoryID (FK)
ArticleComments
-----------------
CommentID
ArticleID (FK)
CommentText
I have an sp to get article info for all articles of a particular category, including a count of comments for an article, but I think it needs improvement. My struggle has resulted in this:
With resultSet AS
(
select
a.ArticleID
, a.ArticleText
, a.CategoryID
, c.CommentCount
from Articles a
Left Outer Join
(Select count(c.CommentID) as CommentCount, c.ArticleID
from Comments c
Group BY c.ArticleID
) c
on a.ArticleID = c.ArticleID
)
select * from resultSet
where resultSet.CategoryID = 2
How should I have written this? I was looking for a way to eliminate the resultSet and the select on the resultSet.
Thanks much for any help Bill