Page 1 of 5 12345 LastLast
Results 1 to 15 of 67
  1. #1
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187

    Query based on result of another query

    I have one table that has a list of people, each with their own 'Role'.



    I need to cycle through this people list, selecting people with the role of 'Attendee' and count how many times they appear in a field of another table 'Events'

    In the Events table, I have a field (Attendees) that contains a list of names (delimited by carriage return)

    I want to end up with a list of names with the # of times they were found in the Attendee fields in Events.

    PeopleTable [Name],[Role]
    Names with Role= 'Attendee'
    Name1
    Name2
    name3
    Name4

    EventsTable [Company],[Date],[Attendees]
    Record 1
    Name2
    Name3

    Record 2
    Name1
    Name2
    Name4

    Record 3
    Name1
    Name2
    Name3

    I want to end up with
    Name1 2
    Name2 3
    Name3 2
    Name4 1

    Code:
    SELECT Count(*)
    FROM [Events],[People]
    WHERE (([Events].[EventDate] >= Date()) AND (InStr([Events].[Attendees],[People].[Name])>0)) 
    IN
    (
    SELECT [People.Name]
    FROM [People]
    WHERE ([People].[Role] = "Attendee") 
    )
    ORDER BY [People].[Name];
    Or

    Code:
    SELECT [People.Name]
    FROM [People]
    WHERE ([People].[Role] = "Attendee")
    IN
    (
    SELECT Count(*)
    FROM [Events],[People]
    WHERE (([Events].[EventDate] >= Date()) AND (InStr([Events].[Attendees],[People].[Name])>0))
    )
    ORDER BY [People].[Name];

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    This would be so easy with an aggregate query if the data were normalized. Even a multi-value field would be manageable and I don't like multi-values. A delimited string is the worst arrangement for this sort of data analysis.

    I could get this to work only with a unique ID field in each table. I used an autonumber type.

    SELECT Name, Count(People.ID) AS CountAttendance
    FROM (SELECT Events.ID, People.ID, Name, Attendees, Role
    FROM Events, People
    WHERE InStr([Attendees],[Name])>0 AND Role="Attendees") AS Q1
    GROUP BY Name;


    BTW, Name is a reserved word and should not use reserved word as fieldname.
    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
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187
    Heres my two working queries that need to be tied together.

    Retrieve list of names to get counts of:
    Code:
    SELECT People.Name
    FROM People
    WHERE ([People].[Access] = "Attendee");
    Count the persons events (Need to replace THENAME with the resulting name from the first query, for each name)
    Code:
    SELECT Count(*)
    FROM Events
    WHERE (InStr([Events].[Attendees],THENAME)>0);

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I was editing my post as you were posting. Might read it again.
    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
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187
    Is this type of query possible? Or should I do this manually in VBA. This report will be ran once a month, so efficiency will not impact the day-to-day of the db, but I'd prefer to do it in SQL if possible.

  6. #6
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187

    Re: Query based on result of another query

    How would I normalize this? Create a separate table to store attendees for each event?

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Did you try the suggested query?

    Yes, a related table would be normalization. They only other approach is a multi-value field, which would look like a comma separated string but is different.
    Last edited by June7; 12-14-2013 at 05:53 PM.
    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.

  8. #8
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187

    Re: Query based on result of another query

    Yes, it runs but I get no results, I'm assuming its because my people table had no id field (I added the field to get it to run).

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    So it does work and now you must decide whether to keep it or normalize db.
    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.

  10. #10
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187

    Re: Query based on result of another query

    What steps are involved in normalizing. I understand it'll involve moving my attendees to a separate table but based on the functionality of my current setup, how can I make it work?

    Now, when attendees are selected from a listbox, the selected names populate in a textbox (via VB code) and this textbox is bound to Attendees field in Events. This is how I get my carriage-return delimited list of attendees.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Normalizing would mean creating and populating table and redesigning queries and forms for data entry, probably with a form/subform arrangement, and reports for data output.
    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
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187

    Re: Query based on result of another query

    Do you think this is something I can do without redoing the entire project? I kind of explained the current arrangement as far as the users and attendees are concerned

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Certainly everything concerning attendees will have to be modified. I have no idea how much you've already developed.
    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.

  14. #14
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187

    Re: Query based on result of another query

    Theres really only 2 other things involving attendees and this is for reports. The guys at utteraccess showed me a way using comboboxes in a sub form to add attendees which I kind of like.

  15. #15
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Is that a multi-value field? I don't use multi-value fields.
    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.

Page 1 of 5 12345 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 11-27-2013, 05:24 PM
  2. Replies: 9
    Last Post: 05-08-2013, 02:37 PM
  3. Replies: 1
    Last Post: 06-08-2012, 11:14 AM
  4. combo box query based on different query result?
    By mejia.j88 in forum Queries
    Replies: 10
    Last Post: 02-15-2012, 02:00 PM
  5. Query result based upon two field conditions
    By diane802 in forum Access
    Replies: 35
    Last Post: 01-08-2010, 06:31 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