Append Query for records that do not exist

Posted on 2014-09-27
Medium Priority
Last Modified: 2014-12-21
I'm trying to tweak my current attendance database for work and make it run quicker for a specific task. I've attached a sample of the functions that I am trying to speed up. Obviously in the sample it runs quick because there is hardly any data for it to search through but when I have it fully loaded with data this particular append query takes hours to run. I need a better solution.

So what I'm trying to do is this....I upload all our employees (115 employees) time clock punches weekly from our payroll system. I then upload all our employees vacation requests and personal time request in there as well. Once all this is uploaded I run the "qryAppendNoShows" query to have it look for employees who did not punch in on a given day. And if they didn't then I have it check those dates against the tblAttendanceEntries to see if they have a scheduled vacation day or personal day for that day. After that I then look at their current work hours in the tblBusinessHours to see if they were scheduled to work on that specific day of the week. Now if they have a scheduled vacation day or personal day then it won't add a record as absent for that day. And if they were not scheduled to work on that day of the week then it won't add a record as well. But if they were scheduled to work that day AND there are no vacation or personal day entries for that day then it adds a record for them being absent that day. There are 2 queries behind the "qryAppendNoShows". the "taNoShow" query looks up in the tblTimeAttendanceEntries to see if there is a day missed in their punches. If it finds a day missing and it's between when their start date was and it's less than the current date then it will show those dates in that query. It matches that against the WorkdaysCalendar table to see if that was an actual workday and not a holiday. The next query is "taNoShowBH". That looks to see if that day, or days is valid based on their work schedule for the week. If it is then those records will show in this query. Then the "qryAppendNoShows" looks to see if there is a vacation day or personal day for that day, or days in the tblAttendanceEntries. If there isn't then it adds an "Unexcused Absence" record for that day, or days. If there is then it doesn't add a record, or records.

The problem I'm having as I said earlier in this post is that it takes hours for this query to run because it's looking through over 60,000 punches for the calendar year for over 100 employees. Then it's looking through 12,000+ attendance entry records. It just takes forever. So what I want to try and do is somehow combine the "taNoShow" query and the "taNoShowBH" query and the "qryAppendNoShows" into one query if possible and speed it up.

The sample DB I've attached has one employee in there as an example. When you run the query "taNoShow" you will see a record for 9/4/14 & 9/5/14 since there were no punches for him for that day. Then when you run the query "taNoShowBH" you will still see those 2 records since he was scheduled to work those days. Then when you run the "qryAppendNoShows" you will only see the 9/5/14 record because he had a vacation day scheduled for 9/4/14. There are other records in the queries as well but these 2 dates show how the example works through all the queries.

Now this was put together about 6 years ago so I'm sure this can be improved on. Any help would be greatly appreciated. I hope I explained it clearly enough but if not feel free to ask any questions.

Question by:Lawrence Salvucci
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 48

Expert Comment

by:Dale Fye
ID: 40348061
I'm on my iPad, so I cannot see your data structure, but the first thing I would do is make sure that you indices defined on your date and employeeID fields.  These will be very critical to the speed of this process.

You might also be able to take advantage of a temporary table (see my article for a description of how to do this, along with a procedure I wrote to make the process very easy).  I think I would try to join the Attendance and Schedule tables as the first step, and copy those records where you have Attendance but not Schedule and Schedule but not Attendance into a temporary table.

Once you have that temp table, it should be much easier and quicker to identify those that don't have an approved day off.

Author Comment

by:Lawrence Salvucci
ID: 40348894
Hi Dale,
Thanks for the quick response. I think I follow you but not totally. What exactly am I dumping into this temp table? I'm kinda stuck on that question before I attempt to create this.
LVL 48

Expert Comment

by:Dale Fye
ID: 40348952
Well, I'm still not at my computer, so bear with me.

Again, I would strongly recommend that you create indices in your Attendance and Scheduling table.  Then I would created a "frustrated outer Join" query to identify people on the schedule for the past week (or whatever period) and who don't have records for the corresponding date in the Attendance table.  Something like:

SELECT S.EmployeeID, S.SchedDate
FROM tblSchedule as S
LEFT JOIN tblAttendance as A
ON S.EmployeeID = A.EmployeeID
AND DateValue(S.SchedDate) = DateValue(A.PunchDateTime)
WHERE S.SchedDate BETWEEN #9/22/14# and #9/27/14#

Because this query does not use a straight [SchedDate] = [PunchDateTime] in the where clause, you will have to edit it in the SQL view.

This will identify individuals in tblSchedule who don't have valid punch times.  Once you have this query, you can change it to a make-table or use the procedure in my temp-tables article to create the temp table (tblUnMatched).  I would expect that this would be a relatively small table, and would then be very easy to compare to your Vacations table, something like:

SELECT UM.EmployeeID, UM.SchedDate
FROM tblUnMatched  as UM <= this is the make table from above
LEFT JOIN  tblVacations as V
ON UM.EmployeeID = V.EmployeeID
AND UM.SchedDate >= V.StartDate
AND UM.SchedDate <= V.EndDate

This should identify those who were scheduled but don't have attendance records, and who don't have approved vacations.  You could then change this from a Select query to an Append query to append these record to your NoShows table.


Author Comment

by:Lawrence Salvucci
ID: 40349682
HI Dale,

Ok I created indices and now I'm onto creating the "frustrated outer join". Do I dare ask why you define it as that?
The problem I think I'm going to have with that is that the tblSchedule only has a starting date for their current hours and does not list every date so how would I create this part of the query: "AND DateValue(S.SchedDate) = DateValue(A.PunchDateTime)". When their hours change I enter a new record in that table with a starting date for those hours and then I put an "EndDate" on the prior record to show when those hours stopped. So when I build this query I will need to see where the PunchDateTime falls in between those 2 dates and see what their hours were at that point in time. For their current hours record I leave the "EndDate" field blank to show their current hours. So I'm not sure how I would write that section of the query.

LVL 48

Accepted Solution

Dale Fye earned 2000 total points
ID: 40349756
"frustrated outer join" = the "frustrated" part is that you are looking for records in one, not found (frustrated) in the other.

OK, so the assumption is that each employee with a Schedule.StartDate and a Null in Schedule.EndDate should show up for work every day between two provided dates.  

I keep a table (tbl_Numbers) in every one of my databases.  It contains a single field (intNumber) and 10 records (the values 0 - 9).  With this table and your Schedule table, you can create a query to identify each of the "Employee/Sched Dates" like:

SELECT S.EmployeeID, DateAdd("d", intNumber, #9/22/2014#) as SchedDate
FROM tblSchedules as S, tbl_Numbers
WHERE (S.[StartDate] IS NOT NULL) AND (S.[EndDate] IS NULL)
AND (DateAdd("d", intNumber, #9/22/2014#) >= S.[StartDate])
AND (DateAdd("d", intNumber, #9/22/2014#) < #9/27/2014)

You will obviously need to change those date parameters for each week as you do this processing, but that should be easy enough.

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question