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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:17:52+00:00 2026-06-19T02:17:52+00:00

I have a table that looks like this: Categories: cId | Name | Parent

  • 0

I have a table that looks like this:

Categories:
cId | Name                    | Parent
----+-------------------------+-------
1   | Parent One              | NULL
2   | Child of 1st Parent     | 1
3   | Parent Two              | NULL
4   | Child of 1st Parent     | 1
5   | Child of 2nd Parent     | 2

The table does not represent a heirarchy: Every item is either a child or a parent, but not both.

And one table like this:

Posts:
pId | Name                    | cID
----+-------------------------+-------
1   | Post 1                  | 1
2   | Post 2                  | 2
3   | Post 3                  | 2
4   | Post 4                  | 3

I’d like to run a query on it that returns this:

cId | Count  
---+---------
1   | 3
2   | 2
3   | 1
4   | 0
5   | 0

Count is the number of posts connected to the category.

All categories should be returned.

Parent categories should have the count of the category + child categories sum. (this is one of the things I’m having problem with)

Child categories should have the category sum.

How should I do this?

  • 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-06-19T02:17:53+00:00Added an answer on June 19, 2026 at 2:17 am

    From your expected results, it looks like you don’t care about grandchildren and lower, in which case, this should work. To get the correct parent count, I’m checking for Parent IS NULL or Count(children) > 0, in which case, I’m adding 1:

    SELECT c.cId, CASE WHEN C.Parent IS NULL OR COUNT(C2.cId) > 0 THEN 1 ELSE 0 END + 
        COUNT(C2.cId) TotalCount
    FROM Categories C
        LEFT JOIN Categories C2 on c.cId = c2.Parent
    GROUP BY c.cId
    

    Here is some sample fiddle: http://www.sqlfiddle.com/#!2/b899f/1

    And the results:

    CID  TOTALCOUNT
    1    3
    2    2
    3    1
    4    0
    5    0
    

    —EDIT—

    From reading your comments, it looks like you want something like this:

    SELECT c.cId, 
        COUNT(DISTINCT P.pId) + COUNT(DISTINCT P2.pId) TotalCount
    FROM Categories C
       LEFT JOIN Posts P ON C.CId = P.CId
        LEFT JOIN Categories C2 on c.cId = c2.Parent
       LEFT JOIN Posts P2 ON C2.CId = P2.CId
    GROUP BY c.cId
    

    http://www.sqlfiddle.com/#!2/eb0d2/3

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

Sidebar

Related Questions

I have a categories table that looks like this: id | name | parent
I have an entity that looks like this: /** * @Gedmo\Tree(type=nested) * @ORM\Table(name=categories) *
Ok so I have a table called categories that looks like this: id name
I have a table that looks like this: table name: uno str_id | title
I have a categories table that looks like this: ---------------------------------------- | id | parentId
I have a database with 2 tables that looks like this: CATEGORIES Name |
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have a table that looks like this Id FirstName 5 Adam 6 Bob

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.