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

  • SEARCH
  • Home
  • 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 6251291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:35:17+00:00 2026-05-24T13:35:17+00:00

I have a database which stores employees leave. For each day an employee is

  • 0

I have a database which stores employees leave. For each day an employee is on leave a new record is entered to the database. What I want to achieve is for someone to enter an employee id and a date range and for each period of absence a record is returned stating date from, date to, duration and also whether it was AM or PM (for half days).

It should look something like (for employee 9999 and dates 2011-08-08 to 2011-09-01):

employee_id | Start      | start_am_pm | End        | end_am_pm | Duration
9999        | 2011-08-10 | PM          | 2011-08-12 | AM        | 2
9999        | 2011-09-01 |             | 2011-09-01 |           | 1

Note: first duration above is 2 because 10th and 12th are both half days and 11th is a full.

Anyway. The query I have works exactly as I would expect, if the From date is not a date that the employee has leave on. Eg, in the above example if I set the from date to 10th, 11th or 12th, it removes that line. It should count the days between the dates specified.

How it currently shows (for employee 9999 and dates 2011-08-11 to 2011-09-01):

employee_id | Start      | start_am_pm | End        | end_am_pm | Duration
9999        | 2011-09-01 |             | 2011-09-01 |           | 1

Similar was happening with the To date but I got that fixed. A similar approach didn’t work for the From date. Below is my stored procedure.

DELIMITER $$

USE `test`$$

DROP PROCEDURE IF EXISTS `GetLeaveDates`$$

CREATE DEFINER=`root`@`%` PROCEDURE `GetLeaveDates`(pEmpID INT, pDateFrom DATETIME, pDateTo DATETIME)
BEGIN

SELECT 
    a.start_date,
CASE WHEN a.am_pm = 1 THEN "AM"
     WHEN a.am_pm = 2 THEN "PM"
     ELSE "" END AS start_am_pm,
    CASE WHEN pDateTo > MIN(c.start_date) THEN
        MIN(c.start_date)
    ELSE
        pDateTo
    END AS End,
CASE WHEN c.am_pm = 1 THEN "AM"
     WHEN c.am_pm = 2 THEN "PM"
     ELSE "" END AS start_am_pm,
    CASE WHEN a.am_pm = 0 AND c.am_pm = 0 THEN
        DATEDIFF(MIN(c.start_date),a.start_date)+1
         WHEN (a.am_pm = 0 AND c.am_pm <> 0) OR (c.am_pm = 0 AND a.am_pm <> 0) THEN
        DATEDIFF(MIN(c.start_date),a.start_date)+0.5
         WHEN a.am_pm <> 0 AND c.am_pm <> 0 THEN
        DATEDIFF(MIN(c.start_date),a.start_date)
    END
     AS Duration
FROM t AS a
LEFT JOIN t AS b ON a.employee_id=b.employee_id AND a.start_date = ADDDATE(b.start_date,1)
LEFT JOIN t AS c ON a.employee_id=c.employee_id AND a.start_date <= c.start_date
LEFT JOIN t AS d ON c.employee_id=d.employee_id AND c.start_date = ADDDATE(d.start_date,-1)
WHERE b.start_date IS NULL AND c.start_date IS NOT NULL AND d.start_date IS NULL
AND a.EMPLOYEE_ID = pEmpID
AND a.START_DATE BETWEEN pDateFrom AND pDateTo
GROUP BY a.employee_id, a.start_date   
; END$$

DELIMITER ;
  • 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-24T13:35:17+00:00Added an answer on May 24, 2026 at 1:35 pm

    Ok I figured it out and it was quite simple, basically on each of the LEFT JOIN I had to filter by out the start date by the parameters passed in.

    I also had to filter by whether the leave was approved, an Approved_DateTime and Approved_By field where filled in if it was approved. Also the calculation of the duration was a bit off in some circumstances. So my stored procedure now looks like:

    DELIMITER $$
    
    USE `test`$$
    
    DROP PROCEDURE IF EXISTS `GetLeaveDates`$$
    
    CREATE DEFINER=`root`@`%` PROCEDURE `GetLeaveDates`(pEmpID INT, pDateFrom DATETIME, pDateTo DATETIME, pApproved BOOLEAN)
    BEGIN
    
    SELECT 
        DATE_FORMAT(a.start_date,'%d/%m/%y') AS start,
    CASE WHEN a.am_pm = 1 THEN "AM"
         WHEN a.am_pm = 2 THEN "PM"
         ELSE "" END AS start_am_pm,
        DATE_FORMAT(CASE WHEN pDateTo > MIN(c.start_date) THEN
            MIN(c.start_date)
        ELSE
            pDateTo
        END, '%d/%m/%y') AS end,
    CASE WHEN c.am_pm = 1 THEN "AM"
         WHEN c.am_pm = 2 THEN "PM"
         ELSE "" END AS end_am_pm,
      CASE WHEN a.am_pm = 0 THEN
        CASE WHEN c.am_pm = 0 OR c.am_pm = 2 THEN
            DATEDIFF(MIN(c.start_date),a.start_date)+1
        WHEN c.am_pm = 1 THEN
            DATEDIFF(MIN(c.start_date),a.start_date)+0.5
        END
    WHEN a.am_pm = 1 THEN
        CASE WHEN c.am_pm = 0 OR c.am_pm = 2 THEN
            DATEDIFF(MIN(c.start_date),a.start_date)+1
        WHEN c.am_pm = 1 THEN
            DATEDIFF(MIN(c.start_date),a.start_date)+0.5
        END
    WHEN a.am_pm = 2 THEN
        CASE WHEN c.am_pm = 0 OR c.am_pm = 2 THEN
            DATEDIFF(MIN(c.start_date),a.start_date)+0.5
        WHEN c.am_pm = 1 THEN
            DATEDIFF(MIN(c.start_date),a.start_date)
        END 
    END AS Duration
    
    FROM t AS a
    LEFT JOIN t AS b ON a.employee_id=b.employee_id AND a.start_date = ADDDATE(b.start_date,1) AND ISNULL(b.approved_datetime) <> pApproved AND b.start_date BETWEEN pDateFrom AND pDateTo
    LEFT JOIN t AS c ON a.employee_id=c.employee_id AND a.start_date <= c.start_date AND ISNULL(c.approved_datetime) <> pApproved AND c.start_date BETWEEN pDateFrom AND pDateTo
    LEFT JOIN t AS d ON c.employee_id=d.employee_id AND c.start_date = ADDDATE(d.start_date,-1) AND ISNULL(d.approved_datetime) <> pApproved AND d.start_date BETWEEN pDateFrom AND pDateTo
    WHERE b.start_date IS NULL AND c.start_date IS NOT NULL AND d.start_date IS NULL
    AND a.EMPLOYEE_ID = pEmpID
    AND a.START_DATE BETWEEN pDateFrom AND pDateTo
    AND ISNULL(a.approved_datetime) <> pApproved
    AND a.start_date BETWEEN pDateFrom AND pDateTo
    GROUP BY a.employee_id, a.start_date
    ; END$$
    
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an EMPLOYEE table in a SQL Server 2008 database which stores information
I have a database which stores (among other things), the following pieces of information:
I have a database table A which stores records, A has a primary key
I have an INT column in a SQL Server database which stores a value
Let's imagine I have a relational database in which, among other things, I want
I have a database which stores the work my staff do. One particular query
I have a database which needs to store year ranges (such as lifespan) which
I have a database which contains picture data stored as a binary blob. The
I have a database in which I'd like to store an arbitrary ordering for
I have a database in my application in which I store something like a

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.