Results 1 to 14 of 14
  1. #1
    khart12 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    6

    Count Records with Multiple Criteria

    I need to determine how many buses complete multiple runs (trips) from the same school.



    I have 3 fields in the DB: Bus No, Run ID and Schools.

    Click image for larger version. 

Name:	DB1.jpg 
Views:	16 
Size:	33.3 KB 
ID:	19558
    To say it in a sentence, I want to know how many Buses have the Same School Code Listed, but also has a Different Run ID.

    In the posted Example - Bus 144 is one such Bus, it visits School 348 three times but has two Run IDs listed (348.105 and 348.106). This would count as "1" instance that I am trying to count. I have hundreds of rows of data for hundreds of buses. I would like to know how many times a Unique Bus Number shows the same school but on different Run IDs. Assuming there were two other such buses in my DB, I would like an Output of "3", indicating 3 buses in the DB make multiple runs from the same school.

    Any tips, tricks or suggestions are greatly appreciated.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You mean the two 348.106 RunIDs would be 1 instance and the 348.105 would be another?

    Does this get you somewhere:

    SELECT BusNo, RunID, Count(Schools) AS CountSchools FROM tablename GROUP BY BusNo, RunID;


    Advise no spaces or special characters/punctuation (underscore is exception) in naming convention.
    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.

  3. #3
    khart12 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    6
    Thanks for the quick response.

    No, I do not need the data tallied in this fashion - "You mean the two 348.106 RunIDs would be 1 instance and the 348.105 would be another?" I only need know that Bus 144 is one bus that meets my criteria.

    Does this get you somewhere: SELECT BusNo, RunID, Count(Schools) AS CountSchools FROM tablename GROUP BY BusNo, RunID; ...
    YES, these commands did work (Thanks) but it didnt tally the data as needed, let me try to better explain what I seek.

    The BusNo in Column 1 has multiple entries (usually 2-6 rows of data). The Schools in Column 3 reflect the schools each Bus Services on a particular Run. The RunID in Column 2 shows which leg of their journey they are on (1st trip, 2nd trip, etc). I need to know how many BusNo in Column 1 list the same Schools in Column 3 but show those Schools on a different RunID in Column 2. So in the generic data I posted originally, Bus 144 is the only such bus in this example. The other Buses should not appear in this Query.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Maybe:

    Query1
    SELECT BusNo, RunID, Schools FROM tablename GROUP BY BusNo, RunID, Schools;

    or
    SELECT DISTINCT BusNo, RunID, Schools FROM tablename;

    Query2
    SELECT BusNo, Schools FROM Query1 GROUP BY BusNo, Schools HAVING Count(RunID) > 1;
    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
    khart12 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    6
    Still not quite there but getting closer... maybe the image below can help to further clarify...

    Click image for larger version. 

Name:	AccessDB_Help.jpg 
Views:	11 
Size:	80.7 KB 
ID:	19574

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Yes, I expected the suggested query sequence to produce that result. However, I do not have data to test. So what was the output of Query2?
    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.

  7. #7
    khart12 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    6
    I did not get to Query2 as Query 1 would not work for me, it would ask me Which School I wanted to search/filter by...which would be all schools.

    I've attached an example DB.

    I appreciate the assistance.

    Bus_DB.zip

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    So what is the correct field name: School or Schools - you used both in your posts.

    Just downloaded the db. Apparently the correct name is School. The queries work.
    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.

  9. #9
    khart12 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    6
    Sorry it took me so long to respond, I wanted to test a few files before I was satisfied. For typical files, this worked great. I really appreciate it.

    However, I did discover one hiccup with this process. See Attached Image and Zipped DB for troubleshooting.

    Click image for larger version. 

Name:	DB2.jpg 
Views:	9 
Size:	84.2 KB 
ID:	19623Bus_DB2.zip

  10. #10
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    hope you don't mind a late comer...

    Try this:
    Save this query as QryPre:
    Code:
    SELECT QMF_PM.BusNo, Left([School],3) AS SchoolNo, Count(QMF_PM.RunID) AS CountOfRunID
    FROM QMF_PM
    GROUP BY QMF_PM.BusNo, Left([School],3);
    Save this query as QryPre2:
    Code:
    SELECT QMF_PM.BusNo, QMF_PM.RunID, QMF_PM.School, Left([school],3) AS SchoolNo
    FROM QMF_PM;
    Save this query as QryFinalPre:
    Code:
    SELECT QryPre.BusNo, QryPre.SchoolNo, QryPre2.RunID, Count(QryPre2.BusNo) AS CountOfBusNo
    FROM QryPre LEFT JOIN QryPre2 ON (QryPre.SchoolNo = QryPre2.SchoolNo) AND (QryPre.BusNo = QryPre2.BusNo)
    GROUP BY QryPre.BusNo, QryPre.SchoolNo, QryPre2.RunID;
    This is your final query:
    Code:
    SELECT QryFinalPre.BusNo, QryFinalPre.SchoolNo, Sum(QryFinalPre.CountOfBusNo) AS SumOfCountOfBusNo
    FROM QryFinalPre
    GROUP BY QryFinalPre.BusNo, QryFinalPre.SchoolNo
    HAVING (((Sum(QryFinalPre.CountOfBusNo))>1));

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The query meets the requirement of "identify bus numbers in column1 show the same school in column3 but not the same runID in column2".

    So if you want only the bus numbers, query3:

    SELECT DISTINCT BusNo FROM Query2;
    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.

  12. #12
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    hah I completely misread this.. I suck

  13. #13
    khart12 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    6
    Works like a charm. Thanks for the assistance.

    Please Mark as SOLVED!

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You can always mark your threads as solved - Thread Tools dropdown above first post.

    Done.
    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.

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

Similar Threads

  1. Replies: 11
    Last Post: 04-13-2020, 02:13 AM
  2. Report summary count with multiple criteria
    By crimedog in forum Reports
    Replies: 1
    Last Post: 09-19-2014, 01:33 PM
  3. Replies: 7
    Last Post: 06-28-2013, 12:15 PM
  4. Replies: 2
    Last Post: 07-29-2012, 05:52 PM
  5. Replies: 2
    Last Post: 05-09-2011, 06:45 PM

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