My database is SQL Server 2000. I am writing a SQL statement to find the number of Sick days taken for this year. My issue is that the Anniversary date is the Hiredate. I just need to know the number of the days taken for this year.
Your problem took some effort to understand, but that's okay, because shorter questions are usually better than longer ones. Without going into all the possible scenarios that you might actually have meant, let's assume that:
the number of days taken refers to the number of sick days taken, which are recorded in a table somewhere, and since each employee can have multiple sick days, there must be multiple rows for each employee, perhaps like:
Sickdays emplID sickDate
so the query needs merely to count the number of rows by emplID for which the sickDate falls within the appropriate range of dates
this year refers to the employee's "anniversary year" which runs from the anniversary date in one calendar year, across the calendar year boundary into the next calendar year, up to the day before that year's anniversary date
The employee's current anniversary year must include today, the current date, but there are actually two possibilities, which are best understood with timeline diagrams.
The first possibility is that the current anniversary year started this year and extends into next year:
<----- this year -----> <----- next year -----> J F M A M J J A S O N D J F M A M J J A S O N D <---- "anniv year" ----> | today
The other possibility is that the current anniversary year started last year and extends into this year:
<----- last year -----> <----- this year -----> J F M A M J J A S O N D J F M A M J J A S O N D <---- "anniv year" ----> | today
At this point, you are probably starting to get concerned about how complex the SQL is going to be. We have the current calendar year, the last calendar year, the next calendar year, the employee's anniversary year, and the current date. Whew!
We know we have to count sick days within the current anniversary year, from the start of the anniversary year up to today, no matter which calendar year those days happen to be in. (Thank goodness we don't also have to count sick days within the anniversary year after the current date! Please email me if it isn't obvious why.)
Actually, now that we have completed our analysis, the problem is pretty easy. All we need is the date that the current anniversary year started.
Please see The current anniversary year (part 2).
Dig deeper on Oracle and SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.