Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7050367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:08:05+00:00 2026-05-28T03:08:05+00:00

I have a forum database that stores forum information in a single column. The

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:08:05+00:00Added an answer on May 28, 2026 at 3:08 am

    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 .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that sends info into a database table. I have it
I have a form with a list that shows information from a database. I
I have certain information being stored in a MySQL database that warrants being stored
i have a form that submits data to a database, i have a function
I have a form that adds links to a database, deletes them, and --
I have a form that shows results from a database query, these results can
I have a method that deletes a row form a database using the sql
I have a web form that manipulates records in a MySQL database. I have
I have an C# form application that use an access database. This application works
I have a control that, upon postback, saves form results back to the database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.