Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3999348
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:38:45+00:00 2026-05-20T07:38:45+00:00

Using SQL Server 2005 Leave Table ID StartDate EndDate 001 04/01/2010 04/02/2010 002 04/02/2010

  • 0

Using SQL Server 2005

Leave Table

ID StartDate EndDate 

001 04/01/2010 04/02/2010
002 04/02/2010 04/03/2010
…

Event Table

ID Date PresentDate Status

001 03/30/2010 03/30/2010 Present
001 03/31/2010 null       absent
001 04/01/2010 null       Leave
001 04/02/2010 null       Leave
001 04/03/2010 null       absent
001 04/04/2010 04/04/2010 Present
….

All the Datecolumn datatype is datetime

In the Status Column, if Present Date is null then it will display as “absent”, if not null then it will display as “present”. Now if we apply a leave for the date then it will display as “Leave” in status column.

Query

Select 
    id, date, present date
    , CASE WHEN t2.id IS NULL THEN t1.Status ELSE ‘Leave’ END AS status 
from event table as t1 
left outer join leave table as t2 on 
    t1.id = t2.id and t1.date between t2.startdate and t2.enddate

The above method is working, but I need to add one more condition.

Once if we applied the leave for the particular employee in the Leave Table then it should compare the Present Date column, if Present Date Column is empty then it should display as “leave”

Expected Output

ID Date PresentDate Status

001 03/30/2010 03/30/2010 Present
001 03/31/2010 null       absent
001 04/01/2010 null       Leave
001 04/02/2010 null       Leave
001 04/03/2010 null       Leave (Expect this value)
001 04/04/2010 04/04/2010 Present
….

From the above output Leave is starting from 04/01/2010 to 04/02/2010, then next column of present date is null then status should display as a “Leave”, once present date is not null then it should display as “Present.

Method

We can display as "Leave" in status column from Start Date to end date of leave table, after that leave date end then we can compare with PresentDate column, if PresentDate column is null then it should display as "Leave", once data is available in present column then status should display with normal condition.

How to make a query for the above condition.

Need Query Help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:38:46+00:00Added an answer on May 20, 2026 at 7:38 am
    select E.id, E.date, E.presentdate, *,
        case
        when E.presentdate is not null then 'Present'
        when E2.presentdate is not null then 'Absent'
        when L.ID is not null then 'Leave'
        else 'Absent'
        end
    from Event E
    outer apply (
        select top 1 *
        from Leave L
        where E.presentdate is null and E.date >= L.startdate
          AND e.ID = L.ID
        order by L.startDate desc) L
    outer apply (
        select top 1 *
        from Event E2
        where E.presentdate is null
          and E2.presentdate is not null
          and E.date >= E2.date and E2.date > L.startdate      
          AND e2.ID = e.ID
        order by E2.presentdate desc) E2
    order by E.date
    

    Leave table

    ID          StartDate               EndDate
    ----------- ----------------------- -----------------------
    1           2010-04-01 00:00:00.000 2010-04-02 00:00:00.000
    1           2010-04-02 00:00:00.000 2010-04-03 00:00:00.000
    1           2010-04-05 00:00:00.000 2010-04-05 00:00:00.000
    

    Output

    id          date                    presentdate             
    ----------- ----------------------- ----------------------- -------
    1           2010-03-30 00:00:00.000 2010-03-30 00:00:00.000 Present
    1           2010-03-31 00:00:00.000 NULL                    Absent
    1           2010-04-01 00:00:00.000 NULL                    Leave
    1           2010-04-02 00:00:00.000 NULL                    Leave
    1           2010-04-03 00:00:00.000 NULL                    Leave -**
    1           2010-04-04 00:00:00.000 2010-04-04 00:00:00.000 Present
    1           2010-04-05 00:00:00.000 NULL                    Leave
    1           2010-04-06 00:00:00.000 NULL                    Leave -**
    1           2010-04-07 00:00:00.000 NULL                    Leave -**
    1           2010-04-08 00:00:00.000 2010-04-08 00:00:00.000 Present
    1           2010-04-09 00:00:00.000 NULL                    Absent
    1           2010-04-10 00:00:00.000 NULL                    Absent
    1           2010-04-11 00:00:00.000 2010-04-11 00:00:00.000 Present
    

    The ones marked -** are not covered by Leave records, but they show leave because they follow a Leave period, correct? 2010-04-09 for example remains “Absent” because it follows a Present record (without actually being present).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2005 Leave Table ID StartDate EndDate 001 02/03/2010 02/03/2010 002 02/03/2010
Using SQL Server 2005 Database1 Table1 ID Date Status 001 23-02-2009 Worked 001 24-02-2009
Using SQL Server 2005 and VB.Net Table1 ID Name Dept 001 Raja IT 002
Using SQL Server 2005 Table1 Date 19-12-2009 20-12-2010 ..... Date Column datatype is DATETIME
Using SQL Server 2005 Table1 ID Time 001 060000 (HHMMSS) 001 080000 002 100000
Using SQL Server 2005 Table1 ID Intime Outtime 001 00.21.00 00.48.00 002 08.23.00 13.45.00
Using SQL Server 2005 Tabl1 ID Date Intime Outtime 001 20101201 080000 180000 001
Using Sql Server 2005 Table1 ID Name Value 001 Rajesh 90 002 Suresh 100
[using SQL Server 2005] I have a table full of users, I want to
Using SQL Server 2005 and 2008. I've got a potentially very large table (potentially

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.