Solved

Count distinct parent and child records

Posted on 2008-10-08
3
463 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

777 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