Recursive query with CTE - SUM of child columns fo

2019-06-23 22:24发布

I have a forum database that stores forum information in a single column. The forum allows for unlimited subforums.

Table name - forums

| ForumID | ParentForumID | Name | Description | TopicCount | ReplyCount | LastPost |

Given a ForumID as a parameter I am trying to SUM the TopicCount and ReplyCount for all child entries. I am also trying to return the latest LastPost, which is specified as DATETIME.

I've searched google and this forum and understand I should be using a recursive CTE but am having some difficulty understanding the syntax. Here is my CTE - work in progress.

   WITH CTE (ForumID, ParentForumID)
   AS
   (
       SELECT ForumID AS Descendant, ParentForumID as Ancestor
       FROM forums
       UNION ALL
       SELECT e.Ancestor
       FROM
          CTE as e
          INNER JOIN CTE AS d
          ON Descendant = d.ParentForumID
   )
   SELECT e.Descendant, SUM(TopicCount) AS topics, SUM(ReplyCount) AS replys
   FROM CTE e
   WHERE e.Ancestor = 1

Where 1 = Parameter for the forum ID.

Thanks in advance for the help!

1条回答
你好瞎i
2楼-- · 2019-06-23 22:46

You're doing OK - you're quite close :-)

Basically, you need to:

  • define the initial forum to be picked before the CTE
  • create an "anchor" query to that forum defined
  • then iterate over all children and sum up the TopicCount and ReplyCount counters

So your code should look something like this:

DECLARE @RootForumID INT
SET @RootForumID = 1  -- or whatever you want...

;WITH CTE AS
(
   -- define the "anchor" query - select the chosen forum
   SELECT 
       ForumID, TopicCount, ReplyCount, LastPost
   FROM 
       dbo.forums
   WHERE
       ForumID = @RootForumID

   UNION ALL

   -- select the child rows
   SELECT 
       f.ForumID, f.TopicCount, f.ReplyCount, f.LastPost
   FROM 
       dbo.forums f
   INNER JOIN
       CTE on f.ParentForumID = CTE.ForumID
)
SELECT 
    SUM(TopicCount) AS topics, 
    SUM(ReplyCount) AS replys,
    MAX(LastPost) AS 'Latest Post' 
FROM 
    CTE

Of course, you could wrap this into a stored procedure that would take the initial "root" ForumID as a parameter .

查看更多
登录 后发表回答