Results 1 to 6 of 6
  1. #1
    joehuggins is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2023
    Posts
    4

    Query that counts first item and then counts within that item

    We have a database that collects data related to corals, specifically how coral grows on rubble.
    We have a table that collects data on each rubble piece. From this table, there is a one-to-many relationship with a coral table, each piece of coral has a unique record. Thus, one piece of rubble may have 0 or more corals at one time.


    We then record a year later whether the unique coral has survived.
    We want to write a query that counts the number of corals on each rubble piece and how many survived one year later.
    Thanks!

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    From this table, there is a one-to-many relationship with a coral table, each piece of coral has a unique record.
    Just to be clear, from your description a piece of coral can only be on one piece of rubble - it can't appear on another piece of rubble? i.e. you will only have one piece of rubble with say elkhorn coral on it.

    But to answer your question based on what you have said, use an aggregate query on your coral table, grouping on the the rubbleFK field and count the rubbleFK for the number. As to how many survived after 12 months, all depends on what data you have to determine - presumably you have some fields to indicated when it first took up residence on the rubble and its state (alive/dead) after twelve months.

  3. #3
    joehuggins is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2023
    Posts
    4
    Thanks, CJ_London (and extra points for knowing your coral species!)!

    Yes, an identified coral can only appear on a unique piece of rubble.

    There is a field, yes/no, that indicates survival after 12 months. So what we want to be able to create is a datasheet that has:
    RubbleID Original_Coral_Count Survival_Count

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Something like:

    SELECT RubbleID, Count("*") AS Original_Coral_Count, Sum(Abs(Survived)) AS Survival_Count
    FROM Rubble INNER JOIN Corals ON Rubble.RubbleID = Corals.RubbleID_FK
    GROUP BY Rubble.RubbleID;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    joehuggins is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2023
    Posts
    4
    Quote Originally Posted by June7 View Post
    Something like:

    SELECT RubbleID, Count("*") AS Original_Coral_Count, Sum(Abs(Survived)) AS Survival_Count
    FROM Rubble INNER JOIN Corals ON Rubble.RubbleID = Corals.RubbleID_FK
    GROUP BY Rubble.RubbleID;
    Thanks June7!

    The 2 tables I am working with are Data_Rubble and Data_Coral. The relevaqnt fields are: Data_Rubble.Rubble_ID and Data_Coral.Coral_ID and Data_Coral.Recruited and Data
    _Coral.FK_Rubble_ID

    Using the query above and converting it to my names this is what I come up with:

    SELECT Data_Rubble.Rubble_ID, Count("*") AS Original_Coral_Count, Sum(Abs(Data_Coral.Recruited)) AS Survival_Count
    FROM Rubble INNER JOIN Corals ON Data_Rubble.Rubble_ID = Data_Coral.FK_Rubble_ID
    GROUP BY Data_Rubble.Rubble_ID;

    I don't quite understand this bit: Count("*") Is something from my data fields supposed to replace the *

    And is this portion correct?

    Thanks

  6. #6
    joehuggins is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2023
    Posts
    4
    Thanks June7!

    I was able to make this work given what you showed.

    I really appreciate your help. Best!

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

Similar Threads

  1. Replies: 6
    Last Post: 05-28-2023, 08:51 PM
  2. Replies: 12
    Last Post: 06-08-2021, 09:46 AM
  3. Replies: 3
    Last Post: 05-04-2021, 07:19 AM
  4. Replies: 7
    Last Post: 11-29-2015, 07:24 AM
  5. Query - Counts
    By maxx102 in forum Access
    Replies: 1
    Last Post: 12-14-2012, 12:49 PM

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