Results 1 to 2 of 2
  1. #1
    BRZ-Ryan is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    187

    Does this query look correct

    My below query works perfect, but if I remove the GROUP BY clause, it returns many duplicates of each name. This makes me think my query is wrong, and possibly not efficient. Can someone take a look to confirm it looks correct. If the group by clause is not expensive to performance then it may be a waste to investigate but I wanted to get an opinion.

    The query populates a combobox with names when an Event is selected. I want all names in the first condition to always appear, and the second condition to use names only if there is an attendance record for the person for the current event in the Attendance table.

    Tables:


    Code:
    [Events]
    ID
    
    [Attendance]
    ID
    EventID
    EmployeeID
    
    [Security]
    ID
    FirstName
    LastName
    Access
    Conditions:
    - If their Access is: 'Attendee', 'Scheduler', 'Admin' (this one is simple)
    OR
    - If their Access is 'Attendee (temp)' AND it finds a record for the person in the Attendance table for the current EventID. I have a command button near the attendee comboboxes to manually add a temp person to the event. When a temp person is added, it writes their name\access to the Security table (if not already exists) and writes and Attendance record with the current eventid and their security id. They should now appear when I navigate to this record, but not in any records where they were not manually added.

    Code:
    SELECT Q1.Security.ID AS EmpID, Q1.Security.FirstName & " " & Q1.Security.LastName AS EmployeeName
    
    FROM (SELECT Security.ID, Security.FirstName, Security.LastName FROM Events, Security, Attendance WHERE ((((Attendance.EmployeeID=Security.ID) AND (txtEventID = Attendance.EventID) AND (Security.Access="Attendee (temp)")) OR (Security.Access IN ("Attendee","Scheduler","Admin"))))) AS Q1
    
    GROUP BY Q1.Security.ID, Q1.Security.FirstName & " " & Q1.Security.LastName
    ORDER BY Q1.Security.FirstName & " " & Q1.Security.LastName;

  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
    If tables have a 1-to-many or many-to-many relationship, duplication of names in this query is expected without the GROUP BY.

    It works if you get the desired result quickly.

    An alternative might be:

    SELECT ID AS EmpID, FirstName & " " & LastName AS EmployeeName FROM Security WHERE ID IN

    (SELECT ID FROM Events, Security, Attendance WHERE (EmployeeID=Security.ID AND txtEventID = EventID AND Access="Attendee (temp)") OR Access IN "Attendee","Scheduler","Admin")

    ORDER BY FirstName, LastName;


    Might be better performance if used JOIN clauses instead of the WHERE to link key fields. Create the links in query designer by clicking and dragging between 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.

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

Similar Threads

  1. Normalisation Query: Which is correct?
    By tim_tims33 in forum Database Design
    Replies: 5
    Last Post: 04-20-2013, 11:31 AM
  2. Replies: 2
    Last Post: 02-25-2013, 09:00 AM
  3. Replies: 2
    Last Post: 01-17-2013, 01:25 AM
  4. Replies: 4
    Last Post: 03-05-2012, 10:20 AM
  5. Replies: 9
    Last Post: 06-26-2011, 09:14 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