Recursive query with CTE - SUM of child columns for a given parent

和自甴很熟 提交于 2019-12-23 12:38: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:


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 .



来源:https://stackoverflow.com/questions/8837347/recursive-query-with-cte-sum-of-child-columns-for-a-given-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!