473,508 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tricky Date Criteria

13 New Member
Hi,

I'm working on an HR database and I want to write a query that counts discipline records for the employees.

Here's the tricky part. Incidents are 'current' for two years. If an employee goes 2 yrs without an incident, their record is wiped clean. If they have an incident within the 2 yr mark, all previous records are counted no matter how far they go back.

Here are my fields:
DisciplineID
EmployeeID
ReviewTypeID (eg. Discipline or Performance)
DiscDate
DiscDescr
WarningID (eg. Safety, Attendance etc.)

Do I start by writing a query that sorts the most recent incident and weeds out people with a clean record?

Any thoughts on how to approach this would be much appreciated.

I'm not great at SQL or VBA so hoping for a simple solution in the query grid.
Dec 13 '12 #1
15 1900
NeoPa
32,557 Recognized Expert Moderator MVP
Hi Dani. Your explanation seems logically inconsistent - from the information posted. If the record is wiped clean when the employee remains clean for two years then any subsequent problem should only go back as far as the point where it was wiped. On the other hand, you suggest that a new offense causes all history to be shown.

I have an idea of how to set this up (with a subquery that groups by the employee and shows the Max of the date all linked in to the main table.) but the first step is always to have a very clear understanding of exactly what's required, so let's get that sorted first.

PS. I should commend you on a clear and well-worded question. Many people never manage such a succinct question :-)
Dec 13 '12 #2
Dani
13 New Member
Obviously I was still a bit muddy, but thank you :)

To clarify, if Employee has an incident in 2009 and nothing else until today, his incident in 09 would not be counted.

If Employee has an incident in 2009 and then in '10 and '11 and today, all previous incidents would be counted.

They are then issued a statement like, "As this is your fourth incident, you are now under suspension" Or "As you have no current incidents on record, you are receiving a written warning."

Basically they have to go a full two years to start clean.

I hope that clears it up. I appreciate any and all advice.

Cheers,
Dani
Dec 13 '12 #3
Rabbit
12,516 Recognized Expert Moderator MVP
I believe NeoPa's question was regarding whether or not the data is purged when their "slate is wiped clean". It sound like the data is still there so their slate is conceptually wiped clean but not actually wiped clean because their infractions are still in the table.
Dec 13 '12 #4
Dani
13 New Member
Excellent comment and not something I had considered doing. My first instinct is to preserve the information, but purging would be more in line with genuinely wiping their record.

The database is still in development stages so this is an excellent discussion for me to have with our HR Manager.

Thank you for this.
Dani
Dec 13 '12 #5
Rabbit
12,516 Recognized Expert Moderator MVP
I just want to say that I was not recommending that the records be deleted, there are arguments for either method. I was merely clarifying NeoPa's question.
Dec 13 '12 #6
NeoPa
32,557 Recognized Expert Moderator MVP
Explaining that a little more clearly (As Rabbit says it's not about deleting data), does data prior to a two year long gap get included if a subsequent infringement/issue is logged?

Consider the following situation where Employee A has incidents on the following dates :
31/1/2005
15/3/2006
1/1/2009
31/8/2009

At the end of 2009 a report is run. Both incidents in 2009 (1/1 and 31/8) are obviously included within the list. Are the previous incidents also included?

Bear in mind that your question stated :
  1. If an employee goes 2 yrs without an incident, their record is wiped clean.
  2. If they have an incident within the 2 yr mark, all previous records are counted no matter how far they go back.

These statements are mutually exclusive, hence we need clarification of the request. We haven't touched on technical details yet (which would include how to extract the information required - when we know what is required).

PS. I appreciate your earlier attempt to clarify, but I'm afraid it didn't really manage to clear up the issue.
Dec 13 '12 #7
Dani
13 New Member
It's a very valid concern about the process, so I thank you for bringing it to my attention. I've now discussed it with the HR Mgr and he has instructed me to purge an employee's record once they have passed the two year mark.

This simplifies the query writing since only employees with records will be in the table.

Thanks for your help Neopa & Rabbit,
cheers,
d
Dec 13 '12 #8
Dani
13 New Member
I missed this comment as I had not refreshed my screen:

"Explaining that a little more clearly (As Rabbit says it's not about deleting data), does data prior to a two year long gap get included if a subsequent infringement/issue is logged?"

No. In this situation they would start again at first Verbal Warning.

From a privacy standpoint, it seems better practice to purge the Employee's record once they're entitled to a clean record so old infractions are not viewed if they are not relevant.

You have both been very helpful in solving this--and without forcing me to use code! Very grateful :)
Dec 13 '12 #9
NeoPa
32,557 Recognized Expert Moderator MVP
Always a pleasure :-)

How do you propose to handle ensuring that such records that need to be purged are done so? How will this process be triggered?

Purging the data certainly makes the process a lot more straightforward of course. It might be possible to achieve without, but certainly not straightforward as far as I can see.
Dec 13 '12 #10
Dani
13 New Member
Good question and one I haven't completely wrestled to the ground.

I think a query to pull the employees with incidents 2 yrs or older and a query to pull all incidents. Then maybe a Find Unmatched? That might be messy, but because I don't know much for code, I tend to take the long walk past Grandma's house to get to where I want to be.

As for triggering it, I've been trying to set up a form so the Main form is the New Incident and a subform shows the Existing Incidents On File.

I want a field that shows the count of Current Incidents and a message that prompts the level of discipline to assigne (eg. Third Written Warning.) (In a perfect database, this would default with the correct InfractionID and show the description "Third Written Warning.")

Perhaps I'll use the AfterUpdate when the user selects the employee from the dropdown. We don't have a ton of these a day so if the DB needs to compute a bit each time the user starts this form, I don't think that's a big issue.

Thanks again for all your help. This is only my second database so I'm still very much a newb. I really appreciate your willingness to coach.
Dec 13 '12 #11
NeoPa
32,557 Recognized Expert Moderator MVP
How about looking at what is necessary to handle the task of removing unwanted records. It's not too trivial.

We start with a query that finds all records where the subsequent date for that employee is greater than two years forward of itself, and returns the [DiscDate] field only.
NB. We will need to run these queries as subqueries of the main DELETE query eventually, so these cannot be saved as separate QueryDefs (Saved Access Queries) unfortunately. I will name them and simplify the explanation by using the names though :
[QueryA]
Expand|Select|Wrap|Line Numbers
  1. SELECT   tD1.DiscDate
  2. FROM     [tblDiscipline] AS [tD1]
  3.          LEFT JOIN
  4.          [tblDiscipline] AS [tD2]
  5.   ON     tD1.EmployeeID=tD2.EmployeeID
  6.  AND     tD1.DiscDate<tD2.DiscDate
  7. WHERE    (DateAdd('yyyy',2,[tD1.DiscDate])<=Date())
  8.   AND    ((tD1.EmployeeID=tD.EmployeeID)
  9.    OR    (tD2.EmployeeID Is Null))
  10. GROUP BY tD1.EmployeeID
  11.        , tD1.DiscDate
  12.        , tD2.EmployeeID
  13. HAVING   (DateAdd('yyyy',2,[tD1.DiscDate])<=Min([tD2.DiscDate]))
  14.     OR   (tD2.EmployeeID Is Null)
Now, that's a query that should (No guarantees at this stage as I have no db to work with here.) return all records for the currently processed employee (See line #10 - [tD] is a refernce from the outside query we'll come to later.) which are two years old or over, and which have no following record within that two year window. We will assume, which I believe is logical, that no employee would have two such gaps still extant within their history. If the process hadn't run at all on the table for more than two years this would be theoretically possible, but I'm hoping that will not be the case here.

Now we need a query to delete all records for an Employee where the [DiscDate] is less than or equal to the value returned by this query :
Expand|Select|Wrap|Line Numbers
  1. DELETE *
  2. FROM   [tblDiscipline] AS [tD]
  3. WHERE  ([DiscDate]<=[QueryA])
A little more straightforward, until they have to be joined together of course. It needs to be seen in context on its own first though, to understand what it's doing. You can now see where [tD] is defined.

The final SQL should be something like :
Expand|Select|Wrap|Line Numbers
  1. DELETE *
  2. FROM   [tblDiscipline] AS [tD]
  3. WHERE  ([DiscDate]<=(SELECT   tD1.DiscDate
  4.                      FROM     [tblDiscipline] AS [tD1]
  5.                               LEFT JOIN
  6.                               [tblDiscipline] AS [tD2]
  7.                        ON     tD1.EmployeeID=tD2.EmployeeID
  8.                       AND     tD1.DiscDate<tD2.DiscDate
  9.                      WHERE    (DateAdd('yyyy',2,[tD1.DiscDate])<=Date())
  10.                        AND    ((tD1.EmployeeID=tD.EmployeeID)
  11.                         OR    (tD2.EmployeeID Is Null))
  12.                      GROUP BY tD1.EmployeeID
  13.                             , tD1.DiscDate
  14.                             , tD2.EmployeeID
  15.                      HAVING   (DateAdd('yyyy',2,[tD1.DiscDate])<=Min([tD2.DiscDate]))
  16.                          OR   (tD2.EmployeeID Is Null)))
Dec 14 '12 #12
Dani
13 New Member
You'll send me a bill? lol

I so appreciate this, NeoPa, thank you. Yes, you are correct that (in theory) no employee would have two gaps in their record. Unless I am extremely careless in loading the data when we go live ;o)

I will try this with my dummy data. Thanks again,
d
Dec 14 '12 #13
NeoPa
32,557 Recognized Expert Moderator MVP
Dani:
You'll send me a bill? lol
I got part of the way through this and thought - "OMG - This is turning into a nightmare. I'd better stop now!"

Then I thought - "After all that work I've already put into this? No. That would be even more depressing."

In the end the hardest work proved to be what I'd already completed by the time I had the first thought :-) Just tidying and padding out after that point really, and putting the explanation together.

I'm happier now I have it to look back on, and I hope it works for you. Bear in mind it was done as air-work, so I had nothing to work on except what was in my head. Luckily, your original question was expressed quite well and clearly - even if there was one little contradiction that held things up for a short while.

Let us know how you get on won't you.
Dec 14 '12 #14
Dani
13 New Member
As said, I'm pretty new to Access so this causes some serious growing pains for me, but it's good to learn and stretch. I really like what Access can do and enjoy trying to solve these little puzzles myself (when they're not above my skill level.)

I've been sidetracked today with a production report form that is sketched out in Word, converted to PDF and combo boxes added. All I can think (screaming in my head) is that this would be so much more efficient if all of these reporting forms were set up and compiled in Access.

But that is a project for after this HR DB is put to bed.

I will get back to you to let you know how the suggestions above worked out.

Thanks again and Happy Friday :)
Dec 14 '12 #15
NeoPa
32,557 Recognized Expert Moderator MVP
I look forward to that Dani.

PS. Fridays are nearly always happy as far as I'm concerned ;-)
Dec 14 '12 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

11
4201
by: David B | last post by:
I have 2 querys with the following in the date criteria Between DateAdd("m",-6,(!!)) And (!!) Between DateAdd("m",-24,(!!)) And DateAdd("m",-6,(!!)) Problem is they both look at the same...
2
1612
by: Rene | last post by:
I have a master "attendance" report that also has two "attendance" sub-reports...each collecting data from a different group...and totaling those present at the end. I chose sub-reports because...
2
1315
by: ljn | last post by:
Hello, I'm trying to build a query to retrieve all records where a certain date is older than 120 days. I tried doing > 120 in the critera box for that date, but it's not working. Any ideas? ...
1
346
by: Mark | last post by:
Hi All, Below is part of a Union query which I can't get to work. I have a pop-up form which asks the user to enter a date and select from 2 option buttons. Once they have done this, the query...
10
3257
by: Daniel | last post by:
In Microsoft Access I can write a query that includes the criteria: Between Date()-7 And Date() to retrieve the records from a table that fall within the last week. I'm trying to write a...
0
1545
by: zeusspandex | last post by:
Im creating a cross tab query which sorts via and sums the for each type of . I want to be able to add a date criteria, so the user can specify that the query processes data between two dates. ...
1
1557
by: zeusspandex | last post by:
Hi, i have the following SQL query and would like to add date criteria to it. the field being from the table and the criteria being Between !! And !! query: SELECT , ...
2
4317
by: kashif73 | last post by:
Hi, I want to export data from 4 Access tables to Excel using VBA. The user selects 2 different DATES from the form & based on DATEs criteria all record from 4 tables should be extracted to...
5
4821
by: sh55555 | last post by:
I have built a query in Access using the DateSerial function to convert dates such as 20100401 to 04/01/2010. The query results work fine and the date is displayed correctly. I am now trying to...
9
6213
by: Raza Zahur | last post by:
Hi all, I've googled and googled but I can google no more, so I am here to ask for help on an issue I am having. I have a macro that runs a series of queries. Before the macro can be run, the...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.