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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:13:04+00:00 2026-06-03T16:13:04+00:00

I have a parent/child/grandchild type table relationship defined as follows: tableA: parentID description tableB:

  • 0

I have a parent/child/grandchild type table relationship defined as follows:

tableA:
parentID
description

tableB:
childID
parentId
description

tableC:
grandchildID
childId
description
itemComplete

I need to write a query that will list all records in tableB for any given parentID, along with the total count of grandchild records and the total count of grandchild records completed (where itemComplete = true).

the parentID will be part of the where clause (select * from tableB where parentId = x). The problem I can’t figure out is how to get the counts from tableC because the count is dependent on the current row value of the childId.

In other words, I need some sort of query that looks like:

select description,  
(select count (*) from tableC where childId = X) as Items,
(select count (*) from tableC where childId = X And itemComplete = true) as CompleteItems
from tableB where parentId=Y

Where X is the current row’s childId from tableB. How do I reference the childId from each row in my sub queries to get the number of items and number of items complete?

  • 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-03T16:13:07+00:00Added an answer on June 3, 2026 at 4:13 pm

    Using subselects is one option but I prefer to JOIN both tables and use a GROUP BY clause using a CASEstatement to get the totals.

    SELECT b.description
           , COUNT(*) AS Items
           , SUM(CASE WHEN c.itemComplete = true THEN 1 ELSE 0 END) AS CompleteItems
    FROM   tableB b
           LEFT OUTER JOIN tableC c ON c.childid = b.childid
    WHERE  b.parentID = 'Y'
    GROUP BY
           b.description
    

    If you insist on using your original statement, all that was missing was the reference to the outer table tableB.childID

    select description,  
    (select count (*) from tableC where childId = tableB.childID) as Items,
    (select count (*) from tableC where childId = tableB.childID And itemComplete = true) as CompleteItems
    from tableB where parentId=Y
    

    or reformatted

    SELECT description
           ,  (SELECT COUNT(*) FROM tableC WHERE childId = tableB.childID) AS Items,
           ,  (SELECT COUNT(*) FROM tableC WHERE childId = tableB.childID AND itemComplete = true) AS CompleteItems
    FROM   tableB 
    WHERE  parentId=Y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent/child table (simple tree) table structure (ID, ParentID), where I want
I have a Parent/Child Table relationship with the ChildTable linking back to the ParentTable
I have a parent child table relationship. In the example below Foo has a
I have a parent/child database relationship between the table ACCOUNT and the table USER.
I have a parent/child relationship via our users table, with models as such: class
If I have entities set up for Parent - Child - Grandchild and one
I have parent/child relationship set up via Node Reference. A Child record can have
I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;
I have a parent-child relationship setup that is fairly basic. The end result is
Using parent child relationship where a parent can have children while each child has

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.