Results 1 to 12 of 12
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310

    Count number of children of a person

    Hi To all,
    I have employees who have children, and the children are in tblChildren that has Primary Key, ChildName, DOB (Birth date).
    What I need to have is:
    1- Total number of children for a specific employee.
    2- Number of children under 18 years old for that employee and
    3- Number of children over 18 years old.

    How can I do This?



    Thank you
    Khalil

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    SELECT tEmployees.Employee, Count(tChildren.ChildID) AS CountOfChildren
    FROM tEmployees INNER JOIN tChildren ON tEmployees.EmpID = tChildren.EmpID
    GROUP BY tEmployees.Employee;

  3. #3
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    437
    you may also try this query:
    Code:
    SELECT 
        EmployeeID, 
        EmployeeName,
         DCount("1", "tblChildren", "EmployeeID = " & [EmployeeID]) As CountOfChildren, 
        DCount("1", "tblChildren", "EmployeeID = " & [EmployeeID] & " And DateDiff('yyyy', [DateOfBirth], Date()) < 18") As CountChildUnder18, 
        DCount("1", "tblChildren", "EmployeeID = " & [EmployeeID] & " And DateDiff('yyyy', [DateOfBirth], Date()) >= 18") As CountChildOver18 
    FROM YourTableName;
    Last edited by jojowhite; 02-25-2025 at 09:41 AM.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    One of those 18s needs an = sign?
    I would create a function for the age, as by years I am 71, but not until July this year.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    the children are in tblChildren that has Primary Key, ChildName, DOB (Birth date).
    it may be your terminology but the table also needs a Foreign Key to link to the Primary Key of the employee table

  6. #6
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180
    Quote Originally Posted by jojowhite View Post
    you may also try this query:
    Code:
    SELECT 
        EmployeeID, 
        EmployeeName,
         DCount("1", "tblChildren", "EmployeeID = " & [EmployeeID]) As CountOfChildren, 
        DCount("1", "tblChildren", "EmployeeID = " & [EmployeeID] & " And DateDiff('yyyy', [DateOfBirth], Date()) < 18") As CountChildUnder18, 
        DCount("1", "tblChildren", "EmployeeID = " & [EmployeeID] & " And DateDiff('yyyy', [DateOfBirth], Date()) > 18") As CountChildOver18 
    FROM YourTableName;
    Never use domain functions in queries. In the above query the engine has to do 3 table scans for each result line.
    Better use 3 groups queries and link them.

  7. #7
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    437
    I agree, Domain functions can cause slowdown, specially for large number of records.
    but it will still be fast if you only have 1000 employees and each employee has 20 childrens.
    and the bonus of using Dfunc() is that your query is updateable.

  8. #8
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    But if you use an INNER join between Employees and Children tables, any employee without children will be removed from the result set. If you want to keep the Employees without children, you need to use an outer join...

    FROM tEmployees LEFT JOIN tChildren...

    or you could confuse people and do it this way... <g>

    Code:
    SELECT e.EmployeeID, e.FirstName, e.LastName, c.AgeGroup, COUNT(c.Birthdate) AS ChildCount
    FROM Employee e 
    LEFT JOIN (
    SELECT FirstName
    	, EmpParentID
    	, BirthDate
    	, AgeGroup = CASE WHEN DATEDIFF(year,Birthdate,GETDATE()) < 18 THEN 'Under 18' ELSE 'OVER 18' END  /* yeah, i know it's wrong... it's close enough */
    FROM Child ) c
    ON e.EmployeeID = c.EmpParentID
    GROUP BY e.EmployeeID, e.FirstName, e.LastName, c.AgeGroup;

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    Could we just not use an IIF() and a function so as to place the age in the correct category?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    yup.

    AgeGroup2 = IIF(DATEDIFF(year,Birthdate,GETDATE()) < 18,'Under 18', 'Over 18')

    Maybe I've been playing with common table expressions for too long.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    Quote Originally Posted by madpiet View Post
    yup.

    AgeGroup2 = IIF(DATEDIFF(year,Birthdate,GETDATE()) < 18,'Under 18', 'Over 18')

    Maybe I've been playing with common table expressions for too long.
    As I mentioned before, that is inaccurate, due to month of birth and current month? That is why I suggested a function. Plenty on the net.

    Here is one someone else wrote
    Code:
    Public Function AgeYears(ByVal datBirthDate As Date) As Integer
      ' Comments: Returns the age in years
      ' Params  : datBirthDate    Date to check
      ' Returns : Number of years
      ' Source  : Total Visual SourceBook
    
      On Error GoTo Proc_Err
    
      Dim intYears As Integer
    
      intYears = Year(Now) - Year(datBirthDate)
    
      If DateSerial(Year(Now), Month(datBirthDate), Day(datBirthDate)) > Now Then
       ' Subtract a year if birthday hasn't arrived this year
        intYears = intYears - 1
      End If
    
      AgeYears = intYears
    
    Proc_Exit:
      Exit Function
    
    Proc_Err:
      MsgBox "Error: " & Err.Number & ". " & Err.description, , "modDateTime.AgeYears"
      Resume Proc_Exit
    End Function
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Hi to all

    Thank all of you for their replies. It was very helpful.

    Khalil

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 06-30-2016, 06:38 PM
  2. Replies: 19
    Last Post: 01-24-2016, 07:48 PM
  3. Replies: 4
    Last Post: 05-30-2012, 08:54 AM
  4. Replies: 1
    Last Post: 03-29-2012, 03:03 PM
  5. Replies: 2
    Last Post: 03-20-2010, 11:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums