Solved

Count distinct parent and child records

Posted on 2008-10-08
3
462 Views
Last Modified: 2010-04-21
I have a query which returns a individual records where a parent-child realtionship exists. I would like to count the distinct parent records and distinct child records. Each parent can have >1 child, but for some reason the number of parent records  = child records which is incorrect. Here is an example

Parent Record 001 + Child Record 001
Parent Record 001 + Child Record 002
Parent Record 001 + Child Record 003

The result I am looking for would be 1 Parent + 3 Child on a single row returned in my query. It should be fairly simple, but I am stumped.
Thanks in advance for your help.
Chad
0
Comment
Question by:cpyatt05
  • 2
3 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 125 total points
ID: 22671217
I'm not sure the question you have asked is really what you want and it involves a few steps but...


To get the distinct parents you would do...

Select distinct parentid from table (save as query1)

For children you do the same thing.

Select distinct childid from table (save as query2)

Then you do a third query:
Select distinct (Select count(*) from query1) as ParentCount, (Select count(*) from query2) as ChildCount from anysmalltable

this gives you counts of distinct parents and distinct children in the table.
0
 

Author Comment

by:cpyatt05
ID: 22671356
I was hoping to do all of this in one query, but your solution works fine. Thanks for your help!
0
 

Author Closing Comment

by:cpyatt05
ID: 31504346
I thought it might be simple. I guess I just couldn't see it. Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now