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 640159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:55:21+00:00 2026-05-13T20:55:21+00:00

I need help with a problem regarding data saved in a parent-children model table

  • 0

I need help with a problem regarding data saved in a parent-children model table and a report I need to build upon it. I’ve already tried searching for topics about parent-children issues, but I couldn’t find anything useful in my scenario.

What I have

A Microsoft SQL Server 2000 database server.

A categories table, which has four columns: category_id, category_name, father_id and visible; the categories have x root categories (where x is variable), and could be y level deep (where y is variable), if a category is a root level one it has father_id null otherwise it’s filled with the id of the father category.

A sales table, which has z columns, one of which is category_id, a foreign key to categories.category_id; a sale must always have a category, and it could be linked anywhere in the aforementioned y level.

What I need

I’ve been asked a report displaying only the root (first level) categories, and the quantity of sales belongings to each of these, or their children, no matter how deep. I.e. if one of the root categories is food, which has a children category named fruit, which has a children category named apple, I need to count every item belonging to food or fruit or apple.

Couldn’t you use the nested set data model?

I know of the nested set model, but I already have the table this way, and migrating it to the nested set model would be a pain (let alone I didn’t even fully grasp how nested set works), not counting the changes needed in the application using the database. (If someone thinks this is still the least pain way, please explain why and how the current data could be migrated.)

Couldn’t you use CTE (Common Table Expressions)?

No, it’s a Microsoft SQL Server 2000, and Common Table Expressions are introduced in the 2005 edition.

Thanks in advance, Andrea.

  • 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-13T20:55:22+00:00Added an answer on May 13, 2026 at 8:55 pm

    SQL 2000 Based solution

    DECLARE @Stack TABLE (
      StackID INTEGER IDENTITY
      , Category VARCHAR(20)
      , RootID INTEGER
      , ChildID INTEGER
      , Visited BIT)
    
    INSERT INTO @Stack
    SELECT  [Category] = c.category_name
            , [RootID] = c.category_id
            , [ChildID] = c.category_id
            , 0
    FROM    Categories c
    
    WHILE EXISTS (SELECT * FROM @Stack WHERE Visited = 0)
    BEGIN
      DECLARE @StackID INTEGER
      SELECT  @StackID = MAX(StackID) FROM    @Stack
    
      INSERT INTO @Stack
      SELECT  st.Category
              , st.RootID
              , c.category_id
              , 0
      FROM    @Stack st
              INNER JOIN Categories c ON c.father_id = st.ChildID  
      WHERE   Visited = 0
    
      UPDATE  @Stack
      SET     Visited = 1
      WHERE   StackID <= @StackID
    END
    
    SELECT  st.RootID
            , st.Category
            , COUNT(s.sales_id)
    FROM    @Stack st
            INNER JOIN Sales s ON s.category_id = st.ChildID
    GROUP BY st.RootID, st.Category
    ORDER BY st.RootID
    

    SQL 2005 Based solution

    A CTE should get you what you want

    • Select each category from Categories to be the root item
    • recursively add each child of every root item
    • INNER JOIN the results with your sales table. As every root is in the result of the CTE, a simple GROUP BY is sufficient to get a count for each item.

    SQL Statement

    ;WITH QtyCTE AS (
      SELECT  [Category] = c.category_name
              , [RootID] = c.category_id
              , [ChildID] = c.category_id
      FROM    Categories c
      UNION ALL 
      SELECT  cte.Category
              , cte.RootID
              , c.category_id
      FROM    QtyCTE cte
              INNER JOIN Categories c ON c.father_id = cte.ChildID
    )
    SELECT  cte.RootID
            , cte.Category
            , COUNT(s.sales_id)
    FROM    QtyCTE cte
            INNER JOIN Sales s ON s.category_id = cte.ChildID
    GROUP BY cte.RootID, cte.Category
    ORDER BY cte.RootID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

I'm into creating a simple contact management application to test how WPF works with
Here's my problem: I have to call a web service with a secure header
I have been given a bit of a strange task, there are around 1500-2000
I'm currently working on a WPF (with C# behind the scenes) system which requires
Ok, so you have something like this working. You Insert into a table from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.