Ask the Expert

The current anniversary year (part 1)

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.

    Requires Free Membership to View

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:

  1. 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

  2. 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).


This was first published in August 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: