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;