Wrox Programmer Forums
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 30th, 2008, 02:47 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default Counting Child Records

I have (in MS Access):
Code:
[u]HULL table</u>
HULL HULT      HULLKEY(PK)
123  'First'     1
345  'Second'    2


[u]PROJ table</u>
PROJ      HULLKEY(FK)  PROJKEY(PK)
'Proj1'    1             10   
'Proj2'    1             11
'Proj3'    2             20
'Proj4'    2             21
'Proj5'    2             22
I want output like:
Code:
HULL HULT      HULLKEY  COUNT(PROJ_RECs_FOR_THIS_HULL)
123  'First'     1         2
345  'Second'    2         3
How can this be done?

I was unsuccessful with
Code:
SELECT [HULL].[HULL],
       [HULL].[HULT],
       [HULL].[HULLKEY],
       [THEPROJ].[PROJCOUNT]
FROM   HULL,
       (SELECT COUNT(PROJ.PROJ) AS PROJCOUNT 
        FROM   PROJ 
        WHERE  PROJ.HULLKEY = HULL.HULLKEY
       ) AS THEPROJ;
       I am prompted for HULLKEY (the bold one, above). The number I enter causes the count for that HULLKEY to be in each row—it’s not dynamic.
 
Old June 2nd, 2008, 04:32 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Code:
SELECT HULL.HULL,
        HULL.HULT,
        HULL.HULLKEY,
        (SELECT COUNT(PROJ.PROJ) 
         FROM   PROJ 
         WHERE  PROJ.HULLKEY = HULL.HULLKEY
        )
FROM   HULL;
This will fail though if the subquery returns more than one record per HULL.HULLKEY. (Count() never returns more than one record, of course.)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using axis instead of count? Counting child nodes. dep XSLT 1 January 17th, 2007 11:27 AM
counting child nodes Tomi XSLT 1 September 6th, 2006 03:26 AM
Counting records Jonas Access 4 August 4th, 2006 09:07 AM
A Cursor counting records chubnut SQL Server 2000 12 July 8th, 2004 01:58 PM
Grouping and Counting records Con Access VBA 1 September 17th, 2003 11:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.