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 7527641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:09:23+00:00 2026-05-30T04:09:23+00:00

I have 2 tables with structure as Emp Table id name 001 Smith 002

  • 0

I have 2 tables with structure as

Emp Table

id    name

001   Smith
002   Jerry


Leave
sr.no   reason      from_date       to_date      request_by   status 
 1      PL          2011-12-11     2011-12-15      001        Declined

 2      PL          2011-11-13     2011-11-13      001        Approved  

 3      PL          2011-10-02     2011-10-05      002        Declined

Now I have written this query

select DATEDIFF(Leave.from_date,Leave.to_date)as cdate,
Emp.id as emp
from Leave left join Emp 
on Leave.request_by=Emp.id

gives me difference between these 2 dates like…

cdate   emp 
 -4     001  
  0     001
 -3     002

The first thing about this output difference between ‘2011-12-11 & 2011-12-15 ‘ need to be 5 as for 5 consecutive days employee is absent. That we achieve it.

But I need this cdate in date format like(‘%Y%m%d’) and + if date difference is say -4 then 4 records should be displayed for that.
So I want to write a query which gives output like this……

cdate            emp
2011-12-11       001
2011-12-12       001 
2011-12-13       001
2011-12-14       001
2011-12-15       001
2011-11-13       001
2011-10-02       002
2011-10-03       002
2011-10-04       002
2011-10-05       002

So can anybody tell me what how should I need to write my query to get this output?

  • 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-30T04:09:24+00:00Added an answer on May 30, 2026 at 4:09 am

    Try this query –

    CREATE TABLE temp_days(d INT(11));
    INSERT INTO temp_days VALUES
      (0),(1),(2),(3),(4),(5),
      (6),(7),(8),(9),(10),
      (11),(12),(13),(14),(15); -- maximum day difference, add more days here
    
    SELECT l.from_date + INTERVAL td.d DAY cdate, e.id emp
    FROM
      `leave` l
    LEFT JOIN Emp e
      ON l.request_by = e.id
    JOIN temp_days td
      ON DATEDIFF(l.to_date, l.from_date) >= td.d
    ORDER BY
      e.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table emp with following structure and data: name dept salary -----
I have two tables that have the exact same structure. Table MasterList Acct_id(9) Name
I have two tables with the same structure: id name 1 Merry 2 Mike
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables with identical structure except for one column... Table 2 has
I have two tables: CATEGORY and SUBCATEGORY Table Structure for CATEGORY category_id int(11) category_name
I have to write a component that re-creates SQL Server tables (structure and data)
I have 2 tables with the following structure.. category { sub_cat_id - numeric sub_cat_name
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I have two tables, the structure of the first partially recapitulates, iterates the structure

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.