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

  • SEARCH
  • Home
  • 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 6109985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:27:55+00:00 2026-05-23T14:27:55+00:00

I have the following tables: Categories ================================= CategoryID | ParentID | Text ——————————— 1

  • 0

I have the following tables:

Categories
=================================
CategoryID |   ParentID   | Text 
---------------------------------
     1           NULL       Text
     2            1         Text
     3           NULL       Text
     4            1         Text

Items
=================================
  ItemID  |  CategoryID  | Text
---------------------------------
     1           1         Text
     2           2         Text
     3           4         Text
     4           3         Text

Bear in mind this is not an n-level hierarchy, the categories have only 2 levels, so no category can have a parentID of 2 for instance.

What I am looking for is a way to return the categories with an extra column which displays the number of items that the category owns (including its subcategories).

i.e. I’m looking for a single query (or procedure) which can return something like the following:

      Categories
============================================
CategoryID |   ParentID   | Text  |  Count
--------------------------------------------
     1           NULL       Text      3
     2            1         Text      1
     3           NULL       Text      1
     4            1         Text      1

My current method of getting the items associated to a category is as follows (Given a categoryID, @CategoryID):

SELECT * FROM Items
WHERE CategoryID 
IN (SELECT CategoryID FROM Categories where ParentID = @CategoryID or CategoryID = @CategoryID)

My problem is that I cannot seem to link this to a select query for the categories themselves.
It’s probably very simple, but I have tried methods using CTE’s, various group by clauses, but the hierarchical nature of the categories seems to throw my logic off.

Thanks for any help!

EDIT: The query also needs to account for categories with no items associated to them

  • 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-23T14:27:56+00:00Added an answer on May 23, 2026 at 2:27 pm

    You’d have to join items to categories, and group by on any fields you need from the category table:

    SELECT  c.CategoryID
    ,       c.ParentID
    ,       c.Text
    ,       count(distinct i.ItemID)
    FROM    Categories c
    LEFT JOIN
            Categories c2
    ON      c.CategoryID = c2.ParentID
    LEFT JOIN    
            Items i
    ON      i.CategoryID = c.CategoryID
            or i.CategoryID = c2.CategoryID
    GROUP BY
            c.CategoryID
    ,       c.ParentID
    ,       c.Text
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables: 1-Categories: -CategoryID -CategoryName -ParentID 2-Items: -ItemId -ItemName -CategoryID categories
I have the following tables: Cateogories CategoryID (int) Primary Key CategoryName (varchar) Items ItemID
I have the following tables CREATE TABLE `files` ( `fileid` int(11) NOT NULL AUTO_INCREMENT,
I have the following in mind: A product can have multiple categories A category
I have the following tables: Persons( PersonID, FirstName, LastNAme) Person_Categories( CategoryID, CategoryName) Persons_PersonCategories( PersonID,
I have the following tables (condensed for readability): Call ID CallerName CallerTime Categories ID
I have inherited a web app with the following tables: Categories, SubCategories and Pages.
I have the following tables in a PostgreSQL: Categories | Locations | Checkins |
I have the following tables: Table categories { -id- -name- 1 Action 2 Comedy
I have the following tables: CATEGORIES -- entry_id, cat_id TITLES -- entry_id, title DATA

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.